{"id":17922710,"url":"https://github.com/overtrue/laravel-like","last_synced_at":"2025-05-14T10:11:37.313Z","repository":{"id":49373203,"uuid":"166324858","full_name":"overtrue/laravel-like","owner":"overtrue","description":"👍 User-like features for Laravel Application.","archived":false,"fork":false,"pushed_at":"2025-03-23T05:02:49.000Z","size":111,"stargazers_count":500,"open_issues_count":0,"forks_count":41,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-04-13T04:56:07.064Z","etag":null,"topics":["laravel-like","laravel-package","laravel-user-relation"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/overtrue.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["overtrue"]}},"created_at":"2019-01-18T01:44:06.000Z","updated_at":"2025-03-28T22:25:36.000Z","dependencies_parsed_at":"2024-02-17T06:31:55.518Z","dependency_job_id":"9d2553f0-3ff6-4e98-8d77-47d3896c7058","html_url":"https://github.com/overtrue/laravel-like","commit_stats":{"total_commits":83,"total_committers":20,"mean_commits":4.15,"dds":"0.37349397590361444","last_synced_commit":"60511dfaf4e900ecc713932606e77b6a29e04bc3"},"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-like","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-like/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-like/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-like/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/overtrue","download_url":"https://codeload.github.com/overtrue/laravel-like/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254109835,"owners_count":22016384,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["laravel-like","laravel-package","laravel-user-relation"],"created_at":"2024-10-28T20:40:32.298Z","updated_at":"2025-05-14T10:11:37.269Z","avatar_url":"https://github.com/overtrue.png","language":"PHP","funding_links":["https://github.com/sponsors/overtrue"],"categories":[],"sub_categories":[],"readme":"# Laravel Like\n\n👍 User-like features for Laravel Application.\n\n[![CI](https://github.com/overtrue/laravel-like/actions/workflows/ci.yml/badge.svg)](https://github.com/overtrue/laravel-like/actions/workflows/ci.yml)\n\n[![Sponsor me](https://github.com/overtrue/overtrue/blob/master/sponsor-me-button-s.svg?raw=true)](https://github.com/sponsors/overtrue)\n\n## Installing\n\n```shell\ncomposer require overtrue/laravel-like -vvv\n```\n\n### Configuration and Migrations\n\n```shell\nphp artisan vendor:publish --provider=\"Overtrue\\LaravelLike\\LikeServiceProvider\"\n```\n\n## Usage\n\n### Traits\n\n#### `Overtrue\\LaravelLike\\Traits\\Liker`\n\n```php\n\nuse Illuminate\\Notifications\\Notifiable;\nuse Illuminate\\Contracts\\Auth\\MustVerifyEmail;\nuse Illuminate\\Foundation\\Auth\\User as Authenticatable;\nuse Overtrue\\LaravelLike\\Traits\\Liker;\n\nclass User extends Authenticatable\n{\n    use Liker;\n\n    \u003c...\u003e\n}\n```\n\n#### `Overtrue\\LaravelLike\\Traits\\Likeable`\n\n```php\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Overtrue\\LaravelLike\\Traits\\Likeable;\n\nclass Post extends Model\n{\n    use Likeable;\n\n    \u003c...\u003e\n}\n```\n\n### API\n\n```php\n$user = User::find(1);\n$post = Post::find(2);\n\n$user-\u003elike($post);\n$user-\u003eunlike($post);\n$user-\u003etoggleLike($post);\n\n$user-\u003ehasLiked($post);\n$post-\u003eisLikedBy($user);\n```\n\nGet user likes with pagination:\n\n```php\n$likes = $user-\u003elikes()-\u003ewith('likeable')-\u003epaginate(20);\n\nforeach ($likes as $like) {\n    $like-\u003elikeable; // App\\Post instance\n}\n```\n\nGet object likers:\n\n```php\nforeach($post-\u003elikers as $user) {\n    // echo $user-\u003ename;\n}\n```\n\nwith pagination:\n\n```php\n$likers = $post-\u003elikers()-\u003epaginate(20);\n\nforeach($likers as $user) {\n    // echo $user-\u003ename;\n}\n```\n\n### Aggregations\n\n```php\n// all\n$user-\u003elikes()-\u003ecount();\n\n// short way\n$user-\u003etotalLikes;\n\n// with type\n$user-\u003elikes()-\u003ewithType(Post::class)-\u003ecount();\n\n// likers count\n$post-\u003elikers()-\u003ecount();\n\n// short way\n$post-\u003etotalLikers\n```\n\nList with `*_count` attribute:\n\n```php\n// likes_count\n$users = User::withCount('likes')-\u003eget();\n\nforeach($users as $user) {\n    // $user-\u003elikes_count;\n}\n\n// likers_count\n$posts = User::withCount('likers')-\u003eget();\n\nforeach($posts as $post) {\n    // $post-\u003elikes_count;\n}\n```\n\n### N+1 issue\n\nTo 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:\n\n```php\n// Liker\n$users = App\\User::with('likes')-\u003eget();\n\nforeach($users as $user) {\n    $user-\u003ehasLiked($post);\n}\n\n// Likeable\n$posts = App\\Post::with('likes')-\u003eget();\n// or\n$posts = App\\Post::with('likers')-\u003eget();\n\nforeach($posts as $post) {\n    $post-\u003eisLikedBy($user);\n}\n```\n\nOf course we have a better solution, which can be found in the following section：\n\n### Attach user like status to likeable collection\n\nYou can use `Liker::attachLikeStatus($likeables)` to attach the user like status, it will attach `has_liked` attribute to each model of `$likeables`:\n\n#### For model\n```php\n$post = Post::find(1);\n\n$post = $user-\u003eattachLikeStatus($post);\n\n// result\n[\n    \"id\" =\u003e 1\n    \"title\" =\u003e \"Add socialite login support.\"\n    \"created_at\" =\u003e \"2021-05-20T03:26:16.000000Z\"\n    \"updated_at\" =\u003e \"2021-05-20T03:26:16.000000Z\"\n    \"has_liked\" =\u003e true\n ],\n```\n\n#### For `Collection | Paginator | LengthAwarePaginator | array`:\n\n```php\n$posts = Post::oldest('id')-\u003eget();\n\n$posts = $user-\u003eattachLikeStatus($posts);\n\n$posts = $posts-\u003etoArray();\n\n// result\n[\n  [\n    \"id\" =\u003e 1\n    \"title\" =\u003e \"Post title1\"\n    \"created_at\" =\u003e \"2021-05-20T03:26:16.000000Z\"\n    \"updated_at\" =\u003e \"2021-05-20T03:26:16.000000Z\"\n    \"has_liked\" =\u003e true\n  ],\n  [\n    \"id\" =\u003e 2\n    \"title\" =\u003e \"Post title2\"\n    \"created_at\" =\u003e \"2021-05-20T03:26:16.000000Z\"\n    \"updated_at\" =\u003e \"2021-05-20T03:26:16.000000Z\"\n    \"has_liked\" =\u003e fasle\n  ],\n  [\n    \"id\" =\u003e 3\n    \"title\" =\u003e \"Post title3\"\n    \"created_at\" =\u003e \"2021-05-20T03:26:16.000000Z\"\n    \"updated_at\" =\u003e \"2021-05-20T03:26:16.000000Z\"\n    \"has_liked\" =\u003e true\n  ],\n]\n```\n\n#### For pagination\n\n```php\n$posts = Post::paginate(20);\n\n$user-\u003eattachLikeStatus($posts);\n```\n\n### Events\n\n| **Event**                             | **Description**                             |\n| ------------------------------------- | ------------------------------------------- |\n| `Overtrue\\LaravelLike\\Events\\Liked`   | Triggered when the relationship is created. |\n| `Overtrue\\LaravelLike\\Events\\Unliked` | Triggered when the relationship is deleted. |\n\n## Related packages\n\n-   Follow: [overtrue/laravel-follow](https://github.com/overtrue/laravel-follow)\n-   Like: [overtrue/laravel-like](https://github.com/overtrue/laravel-like)\n-   Favorite: [overtrue/laravel-favorite](https://github.com/overtrue/laravel-favorite)\n-   Subscribe: [overtrue/laravel-subscribe](https://github.com/overtrue/laravel-subscribe)\n-   Vote: [overtrue/laravel-vote](https://github.com/overtrue/laravel-vote)\n-   Bookmark: overtrue/laravel-bookmark (working in progress)\n\n## :heart: Sponsor me\n\n[![Sponsor me](https://github.com/overtrue/overtrue/blob/master/sponsor-me.svg?raw=true)](https://github.com/sponsors/overtrue)\n\n如果你喜欢我的项目并想支持它，[点击这里 :heart:](https://github.com/sponsors/overtrue)\n\n## Project supported by JetBrains\n\nMany thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.\n\n[![](https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg)](https://www.jetbrains.com/?from=https://github.com/overtrue)\n\n## Contributing\n\nYou can contribute in one of three ways:\n\n1. File bug reports using the [issue tracker](https://github.com/overtrue/laravel-likes/issues).\n2. Answer questions or fix bugs on the [issue tracker](https://github.com/overtrue/laravel-likes/issues).\n3. Contribute new features or update the wiki.\n\n_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._\n\n## PHP 扩展包开发\n\n\u003e 想知道如何从零开始构建 PHP 扩展包？\n\u003e\n\u003e 请关注我的实战课程，我会在此课程中分享一些扩展开发经验 —— [《PHP 扩展包实战教程 - 从入门到发布》](https://learnku.com/courses/creating-package)\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fovertrue%2Flaravel-like","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fovertrue%2Flaravel-like","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fovertrue%2Flaravel-like/lists"}