PHP code example of nanosolutions / laravel-likeable

1. Go to this page and download the library: Download nanosolutions/laravel-likeable 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/ */

    

nanosolutions / laravel-likeable example snippets


'providers' => [
	\Nano\Likeable\LikeableServiceProvider::class,
],

use Nano\Likeable\Likeable;

class Article extends Model {
	use Likeable;
}

$article->like(); // like the article for current user
$article->like($myUserId); // pass in your own user id
$article->like(0); // just add likes to the count, and don't track by user

$article->unlike(); // remove like from the article
$article->unlike($myUserId); // pass in your own user id
$article->unlike(0); // remove likes from the count -- does not check for user

// Dislike (new metric)
$article->like('dislike'); // alias -> $article->dislike();
$article->unlike('dislike'); // alias -> $article->dislike($myUserId);

// Or anything you want (no alias)
$article->like('follow');
$article->unlike('follow');


$article->likeCount; // get count of likes
$article->dislikeCount; // get count of dislikes

$article->likes; // Iterable Illuminate\Database\Eloquent\Collection of existing likes
$article->dislikes; // Iterable Illuminate\Database\Eloquent\Collection of existing disklikes
$article->liked(); // check if currently logged in user liked the article
$article->disliked(); // check if currently logged in user disliked the article
$article->liked($myUserId);

Article::whereLiked($myUserId) // find only articles where user liked them
	->with('likeCounter') // highly suggested to allow eager load
	->get();
bash
php artisan vendor:publish --provider="Nano\Likeable\LikeableServiceProvider" --tag=migrations
php artisan migrate