All Projects → overtrue → Laravel Like

overtrue / Laravel Like

👍 User-like features for Laravel Application.

Projects that are alternatives of or similar to Laravel Like

Laravel Deletable
👾 Gracefully restrict deletion of Laravel Eloquent models
Stars: ✭ 137 (-26.34%)
Mutual labels:  laravel-package
Laravel Auto Translate
Automatically translate your language files using a translator service
Stars: ✭ 153 (-17.74%)
Mutual labels:  laravel-package
Laravel Selfupdater
This package provides some basic methods to implement a self updating functionality for your Laravel application. Already bundled are some methods to provide a self-update mechanism via Github or some private repository via http.
Stars: ✭ 170 (-8.6%)
Mutual labels:  laravel-package
Laravel Api Explorer
API explorer for laravel applications
Stars: ✭ 138 (-25.81%)
Mutual labels:  laravel-package
Servermonitor
💓 Laravel package to periodically monitor the health of your server and application.
Stars: ✭ 148 (-20.43%)
Mutual labels:  laravel-package
Laravel Early Access
This package makes it easy to add early access mode to your existing application.
Stars: ✭ 160 (-13.98%)
Mutual labels:  laravel-package
Twitter
Twitter Notifications Channel for Laravel
Stars: ✭ 135 (-27.42%)
Mutual labels:  laravel-package
Laravel Identify
📦 📱 Laravel 5 Package to Detect Users Browsers, Devices, Languages and Operating Systems
Stars: ✭ 177 (-4.84%)
Mutual labels:  laravel-package
Laravelresources
Speed Up package development for Laravel Apps with API's
Stars: ✭ 152 (-18.28%)
Mutual labels:  laravel-package
Laravel Page Speed
Package to optimize your site automatically which results in a 35%+ optimization
Stars: ✭ 2,097 (+1027.42%)
Mutual labels:  laravel-package
Laravel Scout Postgres
PostgreSQL Full Text Search Engine for Laravel Scout
Stars: ✭ 140 (-24.73%)
Mutual labels:  laravel-package
Laravel Themer
Multi theme support for Laravel application
Stars: ✭ 142 (-23.66%)
Mutual labels:  laravel-package
Laravel Security Checker
Added Laravel functionality to Enlightn Security Checker. Adds a command to check for, and optionally emails you, vulnerabilities when they affect you.
Stars: ✭ 163 (-12.37%)
Mutual labels:  laravel-package
Laravel Easypanel
A beautiful and flexible admin panel creator based on Livewire for Laravel
Stars: ✭ 135 (-27.42%)
Mutual labels:  laravel-package
Laravel Source Encrypter
Laravel and Lumen Source Code Encrypter
Stars: ✭ 175 (-5.91%)
Mutual labels:  laravel-package
Laravel Hashslug
Package providing a trait to use Hashids on a model
Stars: ✭ 136 (-26.88%)
Mutual labels:  laravel-package
Laravel Nist Password Rules
🔒 Laravel validation rules that follow the password related recommendations found in NIST Special Publication 800-63B section 5.
Stars: ✭ 157 (-15.59%)
Mutual labels:  laravel-package
Laravel Adminer
Adminer database manager for Laravel 5+
Stars: ✭ 185 (-0.54%)
Mutual labels:  laravel-package
Laravel Auth Checker
Laravel Auth Checker allows you to log users authentication, devices authenticated from and lock intrusions.
Stars: ✭ 177 (-4.84%)
Mutual labels:  laravel-package
Telegram Bot Sdk
🤖 Telegram Bot API PHP SDK. Lets you build Telegram Bots easily! Supports Laravel out of the box.
Stars: ✭ 2,212 (+1089.25%)
Mutual labels:  laravel-package

Laravel Like

👍 User-like features for Laravel Application.

CI

Installing

$ composer require overtrue/laravel-like -vvv

Configuration

This step is optional

$ php artisan vendor:publish --provider="Overtrue\\LaravelLike\\LikeServiceProvider" --tag=config

Migrations

This step is also optional, if you want to custom likes table, you can publish the migration files:

$ php artisan vendor:publish --provider="Overtrue\\LaravelLike\\LikeServiceProvider" --tag=migrations

Usage

Traits

Overtrue\LaravelLike\Traits\Liker

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Overtrue\LaravelLike\Traits\Liker;

class User extends Authenticatable
{
    use Liker;
    
    <...>
}

Overtrue\LaravelLike\Traits\Likeable

use Illuminate\Database\Eloquent\Model;
use Overtrue\LaravelLike\Traits\Likeable;

class Post extends Model
{
    use Likeable;

    <...>
}

API

$user = User::find(1);
$post = Post::find(2);

$user->like($post);
$user->unlike($post);
$user->toggleLike($post);

$user->hasLiked($post); 
$post->isLikedBy($user); 

Get user likes with pagination:

$likes = $user->likes()->with('likeable')->paginate(20);

foreach ($likes as $like) {
    $like->likeable; // App\Post instance
}

Get object likers:

foreach($post->likers as $user) {
    // echo $user->name;
}

with pagination:

$likers = $post->likers()->paginate(20);

foreach($likers as $user) {
    // echo $user->name;
}

Aggregations

// all
$user->likes()->count(); 

// with type
$user->likes()->withType(Post::class)->count(); 

// likers count
$post->likers()->count();

List with *_count attribute:

// likes_count
$users = User::withCount('likes')->get();

foreach($users as $user) {
    // $user->likes_count;
}

// likers_count
$posts = User::withCount('likers')->get();

foreach($posts as $post) {
    // $post->likes_count;
}

N+1 issue

To avoid the N+1 issue, you can use eager loading to reduce this operation to just 2 queries. When querying, you may specify which relationships should be eager loaded using the with method:

// Liker
$users = App\User::with('likes')->get();

foreach($users as $user) {
    $user->hasLiked($post);
}

// Likeable
$posts = App\Post::with('likes')->get();
// or 
$posts = App\Post::with('likers')->get();

foreach($posts as $post) {
    $post->isLikedBy($user);
}

Events

Event Description
Overtrue\LaravelLike\Events\Liked Triggered when the relationship is created.
Overtrue\LaravelLike\Events\Unliked Triggered when the relationship is deleted.

Related packages

Contributing

You can contribute in one of three ways:

  1. File bug reports using the issue tracker.
  2. Answer questions or fix bugs on the issue tracker.
  3. Contribute new features or update the wiki.

The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable.

PHP 扩展包开发

想知道如何从零开始构建 PHP 扩展包?

请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].