Git Product home page Git Product logo

laravel-like's Introduction

Laravel Like

👍 User-like features for Laravel Application.

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\CanLike

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

class User extends Authenticatable
{
    use Notifiable, CanLike;
    
    <...>
}

Overtrue\LaravelLike\Traits\CanBeLiked

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

class Post extends Model
{
    use CanBeLiked;

    <...>
}

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('likable')->paginate(20);

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

Get user liked items without pagination:

$items = $user->likedItems(); 

foreach ($items as $item) {
    // $item: 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:

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

foreach($users as $user) {
    echo $user->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:

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

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

// CanBeLiked
$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.

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

laravel-like's People

Contributors

dependabot-preview[bot] avatar dependabot-support avatar git-zjx avatar jonasva avatar livijn avatar mingyoung avatar overtrue avatar summerblue 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.