PHP code example of itbm / laravel-likeable

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

    

itbm / laravel-likeable example snippets


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

use Cog\Likeable\Contracts\Likeable as LikeableContract;
use Cog\Likeable\Traits\Likeable;
use Illuminate\Database\Eloquent\Model;

class Article extends Model implements LikeableContract
{
    use Likeable;
}

$article->like(); // current user
$article->like($user->id);

$article->unlike(); // current user
$article->unlike($user->id);

$article->likeToggle(); // current user
$article->likeToggle($user->id);

$article->likesCount;

$article->likesCounter;

$article->likes();

$article->likes;

$article->liked; // current user
$article->liked(); // current user
$article->liked($user->id);

$article->collectLikers();

$article->removeLikes();

$article->dislike(); // current user
$article->dislike($user->id);

$article->undislike(); // current user
$article->undislike($user->id);

$article->dislikeToggle(); // current user
$article->dislikeToggle($user->id);

$article->dislikesCount;

$article->dislikesCounter;

$article->dislikes();

$article->dislikes;

$article->disliked; // current user
$article->disliked(); // current user
$article->disliked($user->id);

$article->collectDislikers();

$article->removeDislikes();

$article->likesDiffDislikesCount;

$article->likesAndDislikes();

$article->likesAndDislikes;

Article::whereLikedBy($user->id)
    ->with('likesCounter') // Allow eager load (optional)
    ->get();

Article::whereDislikedBy($user->id)
    ->with('dislikesCounter') // Allow eager load (optional)
    ->get();

$sortedArticles = Article::orderByLikesCount()->get();
$sortedArticles = Article::orderByLikesCount('asc')->get();

$sortedArticles = Article::orderByDislikesCount()->get();
$sortedArticles = Article::orderByDislikesCount('asc')->get();

$this->app->bind(
    \Cog\Likeable\Contracts\Like::class,
    \App\Models\CustomLike::class
);

$this->app->singleton(
    \Cog\Likeable\Contracts\LikeableService::class,
    \App\Services\CustomService::class
);

$model = app(\Cog\Likeable\Contracts\Like::class);
$service = app(\Cog\Likeable\Contracts\LikeableService::class);
sh
$ php artisan vendor:publish --provider="Cog\Likeable\Providers\LikeableServiceProvider" --tag=migrations
$ php artisan migrate