Skip to content

kilobyteno/laravel-user-guest-like

Repository files navigation

Laravel User Guest Like

Latest Version on Packagist Total Downloads Tests

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.