hayrullah / laravel-likes
This Library improved to record request likes and make statistics for all links which liked.
Requires
- php: >=7.1.0
- laravel/framework: ^5.5|^6.0|^7.0
This package is auto-updated.
Last update: 2024-12-19 05:07:49 UTC
README
Documentation, Installation, and Usage Instructions
See the DOCUMENTATION for detailed installation and usage instructions.
INSTALLATION
$ composer require hayrullah/laravel-likes
- In Laravel >=5.5 this package will automatically get registered.
For older versions, update your
config/app.php
by adding an entry for the service provider.
'providers' => [ // ... Hayrullah\Likes\LikeServiceProvider::class, ];
- Publish the database from the command line:
php artisan vendor:publish --provider="Hayrullah\Likes\LikeServiceProvider"
- Migrate the database from the command line:
php artisan migrate
Models
Your User model should import the Traits/Likability.php
trait and use it, that trait allows the user to like the models.
(see an example below):
use Hayrullah\Likes\Traits\Likability; class User extends Authenticatable { use Likability; }
Your models should import the Traits/Likable.php
trait and use it, that trait have the methods that you'll use to allow the model be likable.
In all the examples I will use the Article model as the model that is 'Likable', thats for example propuses only.
- see an example below:
use Hayrullah\Likes\Traits\Likable; class Article extends Model { use Likable; }
That's it ... your model is now "likable"! Now the User can like models that have the likable trait.
Usage
The models can be liked with and without an authenticated user (see examples below):
Add to likes and remove from likes:
If no param is passed in the like method, then the model will asume the auth user.
$article = Article::first(); $article->addLike(); // auth user added to likes this article $article->removeLike(); // auth user removed from likes this article $article->toggleLike(); // auth user toggles the like status from this article
If a param is passed in the like method, then the model will asume the user with that id.
$article = Article::first(); $article->addLike(5); // user with that id added to likes this article $article->removeLike(5); // user with that id removed from likes this article $article->toggleLike(5); // user with that id toggles the like status from this article
The user model can also add to likes and remove from favrites:
$user = User::first(); $article = Article::first(); $user->addLike($article); // The user added to likes this article $user->removeLike($article); // The user removed from likes this article $user->toggleLike($article); // The user toggles the like status from this article
Return the like objects for the user:
A user can return the objects he marked as like.
You just need to pass the class in the like()
method in the User
model.
$user = Auth::user(); $user->like(Article::class); // returns a collection with the Articles the User marked as like
Return the likes count from an object:
You can return the likes count from an object, you just need to return the likesCount
attribute from the model
$article = Article::first(); $article->likesCount; // returns the number of users that have marked as like this object.
Return the users who marked this object as like
You can return the users who marked this object, you just need to call the likedBy()
method in the object
$article = Article::first(); $article->likedBy(); // returns a collection with the Users that marked the article as like.
Check if the user already liked an object
You can check if the Auth user have already liked an object, you just need to call the isLiked()
method in the object
$article = Article::first(); $article->isLiked(); // returns a boolean with true or false.
Testing
The package have integrated testing, so every time you make a pull request your code will be tested.
Change log
Please see CHANGELOG for more information on what has changed recently.