vuer / notes
模型注释。
1.1.0
2016-10-06 11:45 UTC
This package is auto-updated.
Last update: 2024-09-23 04:39:29 UTC
README
安装
您可以通过使用以下命令通过composer安装此包:
composer require vuer/notes
接下来,您必须安装服务提供者
// config/app.php 'providers' => [ ... Vuer\Notes\NotesServiceProvider::class, ];
发布迁移和配置文件
php artisan vendor:publish
迁移发布后,您可以通过运行迁移来创建注释表
php artisan migrate
如果您想修改模型,可以在注释配置文件(config/notes.php)中更改模型
/*
* The class name of the note model to be used.
*/
'note_model' => \Vuer\Notes\Models\Note::class,
/*
* The class name of the author model to be used.
*/
'author_model' => \App\User::class,
用法
准备您的模型
为了将注释与模型关联,模型必须实现以下特性
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Vuer\Notes\Traits\HasNotes; class User extends Model { use HasNotes; ... }
创建注释
您可以像这样为模型创建注释
$user = User::find(1); $note = $user->createNote(['body' => 'Lorem ipsum...']);
要保存注释作者,您应该添加第二个参数
$note = $user->createNote(['body' => 'Lorem ipsum...'], \Auth::user());
如果您想保存作者名称,需要在作者类中创建 getNotesAuthorName 方法。如果您想删除用户并保留关于注释作者的信息,这将很有用。
<?php namespace App; use Illuminate\Foundation\Auth\User as Authenticatable; use Vuer\Notes\Traits\HasNotes; class User extends Authenticatable { use HasNotes; public function getNotesAuthorName() { return trim(sprintf('%s %s', $this->name, $this->surname)); } }
您可以获取所有注释或1个注释
$notes = $user->notes; $note = $user->note;
您可以使用它来创建模型描述
protected $fillable = [ 'description', ]; public function setDescriptionAttribute($value) { $this->updateOrCreateNote([], ['body' => $value]); } public function getDescriptionAttribute() { return $this->note ? $this->note->body : ''; }