rumspeed / laravel-notes
为您的Eloquent模型添加备注
v1.1.0
2024-07-21 16:16 UTC
Requires
- php: ^8.2
- illuminate/contracts: ^10.0||^11.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.0||^7.10
- orchestra/testbench: ^9.0.0||^8.22.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.3
- phpunit/phpunit: ^11.0.1||^10.5
This package is auto-updated.
Last update: 2024-09-22 13:48:50 UTC
README
轻松地为您的Eloquent模型添加备注。
安装
您可以通过composer安装此包
composer require rumspeed/laravel-notes
您可以使用以下命令发布和运行迁移
php artisan vendor:publish --tag="notes-migrations"
php artisan migrate
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="notes-config"
这是发布配置文件的内容
return [ /* ----------------------------------------------------------------- | Models | ----------------------------------------------------------------- */ 'authors' => [ 'table' => 'users', 'model' => App\Models\User::class, ], 'notes' => [ 'table' => 'notes', 'model' => Rumspeed\LaravelNotes\Models\Note::class, ], ];
用法
首先,使用Rumspeed\LaravelNotes\Traits\HasManyNotes
特性编辑您的Eloquent模型。
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Rumspeed\LaravelNotes\Traits\HasManyNotes; class Post extends Model { use HasManyNotes; // Other stuff ... }
如果您只想为模型管理一条备注,也可以使用Rumspeed\LaravelNotes\Traits\HasOneNote
特性。
为模型添加备注。
您可以在Eloquent模型上调用以下createNote()
方法
$post = App\Post::first(); $note = $post->createNote('Hello world #1');
添加作者/用户
$user = App\User::first(); $post = App\Post::first(); $note = $post->createNote('Hello world #1', $user);
您也可以通过使用getCurrentAuthorId()
来指定如何添加author
id。
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Rumspeed\LaravelNotes\Traits\HasManyNotes; class Post extends Model { use HasManyNotes; // Other stuff ... /** * Get the current author's id. * * @return int|null */ protected function getCurrentAuthorId() { return auth()->id(); } }
获取备注
$post = App\Post::first(); $notes = $post->notes;
注意:
$post->notes
关系属性仅在HasManyNotes
特性中可用。如果您使用的是HasOneNote
特性,请使用$post->note
代替。
获取作者的备注
您也可以通过在User模型中使用Rumspeed\LaravelNotes\Traits\AuthoredNotes
特性来检索所有作者的备注(例如)。
$user = App\User::first(); $post = App\Post::first(); $post->createNote('Hello world #1', $user); $notes = $user->authoredNotes;
查找具有特定ID的备注
$post = App\Post::first(); $note = $post->findNote(1);
注意:
findNote()
方法仅在HasManyNotes
特性中可用。
测试
composer test
变更日志
有关最近更改的更多信息,请参阅变更日志。
贡献
有关详细信息,请参阅贡献指南。
安全漏洞
有关如何报告安全漏洞的详细信息,请参阅我们的安全策略。
致谢
许可
MIT许可(MIT)。有关更多信息,请参阅许可文件。