kilobyteno / laravel-user-guest-like
A Laravel package to allow guests and users to like models
Fund package maintenance!
danielrtrd
Requires
- php: ^8.0
- illuminate/contracts: ^8.0|^9.0|^10.0
- spatie/laravel-package-tools: ^1.15.0
Requires (Dev)
- nunomaduro/collision: ^5.10|^6.0|^7.0
- orchestra/testbench: ^6.22|^7.0|^8.0
- pestphp/pest: ^1.23
- pestphp/pest-plugin-laravel: ^1.3
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.32
README
A Laravel package to allow guests and users to like models.
Installation
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, ];
Usage
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();
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.