1. Go to this page and download the library: Download cslant/laravel-like 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/ */
cslant / laravel-like example snippets
use CSlant\LaravelLike\Facades\Like;
use CSlant\LaravelLike\Facades\Love;
$post->like(); // or Like::like($post)
Like::dislike($post);
Like::unlike($post);
Like::toggle($post);
Like::isLiked($post); // bool
Like::isDisliked($post); // bool
Like::likesCount($post); // int
Like::dislikesCount($post); // int
Love::love($post); // or $post->love()
Love::unlove($post);
Love::isLoved($post); // bool
Love::lovesCount($post); // int
Like::like($post, $userId);
use CSlant\LaravelLike\HasLike;
class Post extends Model
{
use HasLike;
}
use CSlant\LaravelLike\Facades\Like;
use CSlant\LaravelLike\Enums\InteractionTypeEnum;
$posts = Post::limit(50)->get();
// One query total instead of one per post
$counts = Like::likeCountsFor($posts, InteractionTypeEnum::LIKE); // [postId => count]
// One query total for the current user's state across all posts
$interactions = Like::userInteractionsFor($posts, auth()->id()); // keyed by "morphClass:modelId"
$posts = Post::withCount(['likesTo as likes_count', 'dislikesTo as dislikes_count'])->get();
$models = Like::userLikedModels($userId); // 1 + (number of model types) queries
$user->likes() // hasMany, from the UserHasInteraction trait
->with('model') // eager-load the interactable model
->get();