定制 muzidudu/laravel-record 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

muzidudu/laravel-record

Composer 安装命令:

composer require muzidudu/laravel-record

包简介

User record features for Laravel Application.

README 文档

README

❤️ User record feature for Laravel Application.

CI Latest Stable Version Latest Unstable Version Total Downloads License

Sponsor me

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

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.

❤️ Sponsor me

Sponsor me

如果你喜欢我的项目并想支持它,点击这里 ❤️

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

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-10-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固