Giter Club home page Giter Club logo

laravel-user-guest-like's Introduction

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.

laravel-user-guest-like's People

Contributors

dependabot[bot] avatar dsbilling avatar github-actions[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.