rumspeed/laravel-notes

为您的Eloquent模型添加备注

v1.1.0 2024-07-21 16:16 UTC

This package is auto-updated.

Last update: 2024-09-22 13:48:50 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

轻松地为您的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)。有关更多信息,请参阅许可文件