src/Services/LikeableService.php
public function addLikeTo(LikeableContract $likeable, $type, $userId)
    {
        $userId = $this->getLikerUserId($userId);

        $like = $likeable->likesAndDislikes()->where([
            'user_id' => $userId,
        ])->first();

        if (!$like) {
            $likeable->likes()->create([
                'user_id' => $userId,
                'type_id' => $this->getLikeTypeId($type),
            ]);

            return;
        }

        if ($like->type_id == $this->getLikeTypeId($type)) {
            return;
        }

        $like->delete();

        $likeable->likes()->create([
            'user_id' => $userId,
            'type_id' => $this->getLikeTypeId($type),
        ]);
    }
public function addLikeTo(LikeableContract $likeable, $type, $userId)
    {
        $userId = $this->getLikerUserId($userId);

        $like = $likeable->likesAndDislikes()->where([
            'user_id' => $userId,
        ])->first();

        if (!$like) {
            $likeable->likes()->create([
                'user_id' => $userId,
                'type_id' => $this->getLikeTypeId($type),
            ]);

            return;
        }

        if ($like->type_id == $this->getLikeTypeId($type)) {
            return;
        }

        $like->delete();

        $likeable->likes()->create([
            'user_id' => $userId,
            'type_id' => $this->getLikeTypeId($type),
        ]);
    }
public function addLikeTo ( LikeableContract $ likeable , $ type , $ userId ) { $ userId = $ this -> getLikerUserId ( $ userId ) ; $ like = $ likeable -> likesAndDislikes ( ) -> where ( [ 'user_id' => $ userId , ] ) -> first ( ) ; if ( ! $ like ) { $ likeable -> likes ( ) -> create ( [ 'user_id' => $ userId , 'type_id' => $ this -> getLikeTypeId ( $ type ) , ] ) ; return ; } if ( $ like -> type_id == $ this -> getLikeTypeId ( $ type ) ) { return ; } $ like -> delete ( ) ; $ likeable -> likes ( ) -> create ( [ 'user_id' => $ userId , 'type_id' => $ this -> getLikeTypeId ( $ type ) , ] ) ; }
Add a like to likeable model by user.

@param \Cog\Likeable\Contracts\Likeable $likeable
@param string $type
@param string $userId
@return void

@throws \Cog\Likeable\Exceptions\LikerNotDefinedException
@throws \Cog\Likeable\Exceptions\LikeTypeInvalidException
Add a like to likeable model by user .