digitalcloud/laravel-model-notes

一个允许为Eloquent模型分配备注的软件包

v1.1 2019-01-23 06:47 UTC

This package is auto-updated.

Last update: 2024-09-14 22:29:40 UTC


README

此软件包允许您为模型添加备注。

描述

假设您正在处理模型,比如发票模型,并且这个发票需要通过一些管理流程。每个管理状态都需要经理为这个发票添加一些备注,并且您需要跟踪这个发票的所有备注。此软件包允许您通过仅为模型添加一些特性来完成这项工作。

为Eloquent模型添加备注

此软件包提供了一个HasNotes特性,一旦安装到模型上,您就可以执行如下操作

// add a note
$model->setNote('New Note Here');

// get all notes
$model->notes();

安装

您可以通过composer安装此软件包

composer require digitalcloud/laravel-model-notes

您必须使用以下命令发布迁移

php artisan vendor:publish --provider="DigitalCloud\ModelNotes\ModelNotesServiceProvider" --tag="migrations"

迁移notes

php artisan migrate

可选地,您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="DigitalCloud\ModelNotes\ModelNotesServiceProvider" --tag="config"

这是将发布到config/models-notes.php的文件内容

return [

    /*
     * The class name of the note model that holds all notes.
     * 
     * The model must be or extend `DigitalCloud\ModelNotes\Note::class`.
     */
    'note_model' => DigitalCloud\ModelNotes\Note::class,

    /*
     * The name of the column which holds the ID of the model related to the notes.
     *
     * You can change this value if you have set a different name in the migration for the notes table.
     */
    'model_primary_key_attribute' => 'model_id',

];

用法

HasNotes特性添加到您想要使用备注的模型中。

use DigitalCloud\ModelNotes\HasNotes;

class YourEloquentModel extends Model
{
    use HasNotes;
}

添加新备注

您可以通过以下方式添加新备注

$model->setNote('new note');

检索备注

您可以通过以下方式获取模型的所有关联备注

$allNotes = $model->notes;

自定义模型和迁移

您可以通过在model-notes配置文件的note_model键中指定类名来更改使用的模型。

当使用自定义迁移更改时,您还可以更改备注表中使用的列名(默认为model_id)。在这种情况下,只需更改model-notes配置文件中的model_primary_key_attribute键即可。