akshaykhale1992/model-note

用于向 Laravel 模型添加注释的包

dev-master 2018-09-09 07:43 UTC

This package is auto-updated.

Last update: 2024-09-09 20:32:52 UTC


README

Scrutinizer Score Scrutinizer Build Status

简介

Model Notes 是一个简单的 Laravel 扩展包,可用于向任何 Laravel Eloquent 模型添加注释或评论。

安装

您可以使用 composer 下载此包。您可以使用以下命令进行操作。

composer require akshaykhale1992/model-note:dev-master

用法

此包包含两个特质 notablecreatable。应将 notable 用于您想添加注释的模型中,例如 Post、Article、Video 等。应将 creatable 用于能够创建注释的模型中,例如 User、Admin、Employee 等。

请参考以下简单的 User 和 Post 类示例

app/User.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

use AkshayKhale1992\ModelNote\Creatable;

class User extends Authenticatable
{
    use Notifiable;
    use Creatable; // Able to add Notes

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

App/Post.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

use AkshayKhale1992\ModelNote\Notable;

class Post extends Model
{
    use Notable; // Can have Notes
}

向 Post 添加注释

$post = \App\Post::find(3); // Getting the Post to Add Note
$user = \App\User::find(1); // Getting the User who is adding the Note
$post->addNote($user, "This is a test Note"); // Adding the Note

或者

$post = \App\Post::find(3); // Getting the Post to Add Note
$user = \App\User::find(1); // Getting the User who is adding the Note
$user->addingNoteTo($post, "This is a test Note"); // Adding the Note

从 Post 获取注释

$post = \App\Post::find(3); // Selecting the Post
$user = \App\User::find(1); // Getting the User who has added the Notes
$notes = $post->getNotesBy($user); // Getting notes on a Post by User

或者

$post = \App\Post::find(3); // Selecting the Post
$user = \App\User::find(1); // Getting the User who has added the Notes
$notes = $user->getNotesOn($post); // Getting notes on a Post by User

许可证

开源软件,许可协议为 MIT 许可证

感谢