laravel-record maintained by muzidudu
Laravel Record
❤️ User record feature for Laravel Application.
Installing
composer require muzidudu/laravel-record -vvv
Configuration & Migrations
php artisan vendor:publish
Usage
Traits
Muzidudu\LaravelRecord\Traits\Recorder
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Muzidudu\LaravelRecord\Traits\Recorder;
class User extends Authenticatable
{
use Recorder;
<...>
}
Muzidudu\LaravelRecord\Traits\Recordable
use Illuminate\Database\Eloquent\Model;
use Muzidudu\LaravelRecord\Traits\Recordable;
class Post extends Model
{
use Recordable;
<...>
}
API
$user = User::find(1);
$post = Post::find(2);
$user->record($post);
$user->unrecord($post);
$user->toggleRecord($post);
$user->getRecordItems(Post::class)
$user->hasRecorded($post);
$post->hasBeenRecordedBy($user);
Get object recorders:
foreach($post->recorders as $user) {
// echo $user->name;
}
Get Record Model from User.
Used Recorder Trait Model can easy to get Recordable Models to do what you want.
_note: this method will return a Illuminate\Database\Eloquent\Builder _
$user->getRecordItems(Post::class);
// Do more
$favortePosts = $user->getRecordItems(Post::class)->get();
$favortePosts = $user->getRecordItems(Post::class)->paginate();
$favortePosts = $user->getRecordItems(Post::class)->where('title', 'Laravel-Record')->get();
Aggregations
// all
$user->records()->count();
// with type
$user->records()->withType(Post::class)->count();
// recorders count
$post->recorders()->count();
List with *_count attribute:
$users = User::withCount('records')->get();
foreach($users as $user) {
echo $user->records_count;
}
// for Recordable models:
$posts = Post::withCount('recorders')->get();
foreach($posts as $post) {
echo $post->records_count;
}
Attach user record status to recordable collection
You can use Recorder::attachRecordStatus($recordables) to attach the user record status, it will set has_recorded attribute to each model of $recordables:
For model
$post = Post::find(1);
$post = $user->attachRecordStatus($post);
// result
[
"id" => 1
"title" => "Add socialite login support."
"created_at" => "2021-05-20T03:26:16.000000Z"
"updated_at" => "2021-05-20T03:26:16.000000Z"
"has_recorded" => true
],
For Collection | Paginator | LengthAwarePaginator | array:
$posts = Post::oldest('id')->get();
$posts = $user->attachRecordStatus($posts);
$posts = $posts->toArray();
// result
[
[
"id" => 1
"title" => "Post title1"
"created_at" => "2021-05-20T03:26:16.000000Z"
"updated_at" => "2021-05-20T03:26:16.000000Z"
"has_recorded" => true
],
[
"id" => 2
"title" => "Post title2"
"created_at" => "2021-05-20T03:26:16.000000Z"
"updated_at" => "2021-05-20T03:26:16.000000Z"
"has_recorded" => false
],
[
"id" => 3
"title" => "Post title3"
"created_at" => "2021-05-20T03:26:16.000000Z"
"updated_at" => "2021-05-20T03:26:16.000000Z"
"has_recorded" => true
],
]
For pagination
$posts = Post::paginate(20);
$user->attachRecordStatus($posts);
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:
// Recorder
$users = User::with('records')->get();
foreach($users as $user) {
$user->hasRecorded($post);
}
// Recordable
$posts = Post::with('records')->get();
// or
$posts = Post::with('recorders')->get();
foreach($posts as $post) {
$post->isRecordedBy($user);
}
Events
| Event | Description |
|---|---|
Muzidudu\LaravelRecord\Events\Recorded |
Triggered when the relationship is created. |
Muzidudu\LaravelRecord\Events\Unrecorded |
Triggered when the relationship is deleted. |
Related packages
- Follow: overtrue/laravel-follow
- Like: overtrue/laravel-like
- Record: overtrue/laravel-record
- Subscribe: overtrue/laravel-subscribe
- Vote: overtrue/laravel-vote
- Bookmark: overtrue/laravel-bookmark (working in progress)
Contributing
You can contribute in one of three ways:
- File bug reports using the issue tracker.
- Answer questions or fix bugs on the issue tracker.
- 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.
:heart: Sponsor me
如果你喜欢我的项目并想支持它,点击这里 :heart:
Project supported by JetBrains
Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.
PHP 扩展包开发
想知道如何从零开始构建 PHP 扩展包?
请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》
License
MIT