diff --git a/extensions/likes/extend.php b/extensions/likes/extend.php index 76829338b..47fa4fc14 100644 --- a/extensions/likes/extend.php +++ b/extensions/likes/extend.php @@ -14,6 +14,8 @@ use Flarum\Likes\Event\PostWasLiked; use Flarum\Likes\Event\PostWasUnliked; use Flarum\Likes\Listener; use Flarum\Likes\Notification\PostLikedBlueprint; +use Flarum\Post\Post; +use Flarum\User\User; use Illuminate\Contracts\Events\Dispatcher; return [ @@ -24,6 +26,9 @@ return [ (new Extend\Frontend('admin')) ->js(__DIR__.'/js/dist/admin.js'), + (new Extend\Model(Post::class)) + ->belongsToMany('likes', User::class, 'post_likes', 'post_id', 'user_id'), + function (Dispatcher $events) { $events->subscribe(Listener\AddPostLikesRelationship::class); $events->subscribe(Listener\SaveLikesToDatabase::class); diff --git a/extensions/likes/src/Listener/AddPostLikesRelationship.php b/extensions/likes/src/Listener/AddPostLikesRelationship.php index c61894d28..bf18d7baf 100755 --- a/extensions/likes/src/Listener/AddPostLikesRelationship.php +++ b/extensions/likes/src/Listener/AddPostLikesRelationship.php @@ -15,9 +15,6 @@ use Flarum\Api\Event\WillGetData; use Flarum\Api\Serializer\BasicUserSerializer; use Flarum\Api\Serializer\PostSerializer; use Flarum\Event\GetApiRelationship; -use Flarum\Event\GetModelRelationship; -use Flarum\Post\Post; -use Flarum\User\User; use Illuminate\Contracts\Events\Dispatcher; class AddPostLikesRelationship @@ -27,23 +24,11 @@ class AddPostLikesRelationship */ public function subscribe(Dispatcher $events) { - $events->listen(GetModelRelationship::class, [$this, 'getModelRelationship']); $events->listen(GetApiRelationship::class, [$this, 'getApiAttributes']); $events->listen(Serializing::class, [$this, 'prepareApiAttributes']); $events->listen(WillGetData::class, [$this, 'includeLikes']); } - /** - * @param GetModelRelationship $event - * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany|null - */ - public function getModelRelationship(GetModelRelationship $event) - { - if ($event->isRelationship(Post::class, 'likes')) { - return $event->model->belongsToMany(User::class, 'post_likes', 'post_id', 'user_id', null, null, 'likes'); - } - } - /** * @param GetApiRelationship $event * @return \Tobscure\JsonApi\Relationship|null