PHP code example of nr-type / like-dislike

1. Go to this page and download the library: Download nr-type/like-dislike library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nr-type / like-dislike example snippets


php artisan migrate

use NrType\LikeDislike\Traits\Likeable;
use NrType\LikeDislike\Traits\Dislikeable;

class Post extends Model
{
    use Likeable, Dislikeable;
}

use NrType\LikeDislike\Traits\Likeable;
use NrType\LikeDislike\Traits\Dislikeable;

class Comment extends Model
{
    use Likeable, Dislikeable;
}

use NrType\LikeDislike\Traits\Likeable;

class Post extends Model
{
    use Likeable;
}

public function like (Post $post)
{
    $post->like();

    return redirect()->route('posts.index');
}

public function like (Post $post)
{
    if($post->removeLike()){
        return redirect()->route('posts.index');
    }

    $post->like();

    return redirect()->route('posts.index');
}

public function dislike (Post $post)
{
    $post->dislike();

    return redirect()->route('posts.index');
}

public function dislike (Post $post)
{
    if ($post->removeDislike()) {
        return redirect()->route('posts.index');
    }

    $post->dislike();

    return redirect()->route('posts.index');
}

public function likers (Post $post)
{
    return $post->likers(); 
}

public function dislikers (Post $post)
{
    return $post->dislikers(); 
}

public function likers (Post $post)
{
    $fields = ['id','first_name','last_name','age'];

    return $post->likers($fields); 
}

public function dislikers (Post $post)
{
    $fields = ['id','name','age'];

    return $post->dislikers($fields); 
}

public function index()
{
    $relations = ['likes','likeCounter','dislikes','dislikeCounter'];

    $data['posts'] = Post::with($relations)->get();

    return view('post.index', $data);
}

public function index()
{
    $relations = [
        'likes',
        'likeCounter',
        'dislikes',
        'dislikeCounter',
        'comments.likes',
        'comments.likeCounter'
    ];

    $data['posts'] = Post::with($relations)->get();

    return view('post.index', $data);
}

{{ $post->hasLike() ? 'Liked' : 'Like' }}
{{ $post->hasDislike() ? 'Disliked' : 'Dislike' }}