A Laravel package to allow guests and users to like models.
You can install the package via composer:
composer require kilobyteno/laravel-user-guest-like
Publish the package:
php artisan vendor:publish --provider="Kilobyteno\LaravelUserGuestLike\LaravelUserGuestLikeServiceProvider"
Or you can publish manually:
php artisan vendor:publish --tag="user-guest-like-config"
php artisan vendor:publish --tag="user-guest-like-migrations"
php artisan migrate
The content of the config file that will be published to config/user-guest-like.php
:
return [
// Let guests like a model
'guest_like_enabled' => true,
// Save IP and user agent to database
'user_tracking_enabled' => false,
];
Add the HasUserGuestLike
trait to the model:
use Kilobyteno\LaravelUserGuestLike\Traits\HasUserGuestLike;
use Illuminate\Database\Eloquent\Model;
class EloquentModel extends Model
{
use HasUserGuestLike;
}
Like a model as user (or guest):
$user = auth()->check() ? auth()->user() : null;
// Passing the user as null will like as a guest (if enabled)
$model->like($user);
Dislike a model as user (or guest):
$user = auth()->check() ? auth()->user() : null;
// Passing the user as null will like as a guest (if enabled)
$model->dislike($user);
Check if a user has liked a model (or guest has liked a model):
$user = auth()->check() ? auth()->user() : null;
// Passing the user as null will check if the guest (if enabled) has liked the model
if($model->hasLiked($user)) {
// User has liked the model
}
Display the number of likes for a model:
$model->likes()->count();
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.