andach/laravel-attachments-comments

这是一个Laravel包,允许您向模型添加评论和附件。

v1.0.1 2023-09-27 11:18 UTC

This package is auto-updated.

Last update: 2024-09-27 13:19:55 UTC


README

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

这是一个用于向任何模型添加附件和评论的Laravel包。

安装

您可以通过composer安装此包

composer require andach-limited/laravel-attachments-comments

然后使用以下命令发布配置和迁移

php artisan attachments-comments:install

用法

使用特性

要将关系添加到模型,只需将 MorphToAttachmentsAndComments 特性添加到模型。如果只需要一个,则可以使用 MorphToAttachmentsMorphToComments

class MyModel extends Model
{
    use MorphToAttachmentsAndComments;

    // ...
}

添加附件和/或评论

要实际添加附件或评论,可以使用 addAttachmentAndComment()。它接受一个字符串和一个可选的UploadedFile。

<form action="{{ route('my-model.store') }}" method="POST" enctype="multipart/form-data">
    @csrf

    <input type="text" name="comment" />
    <input type="file" name="attachment" />

    <button type="submit">Submit</button>
</form>

在相关控制器方法中...

public function store(Request $request)
{
    $model = MyModel::create();

    $model->addAttachmentAndComment($request->comment, $request->attachment);

    return redirect()->route('my-model.show', $model);
}

如果传递空字符串或null附件,系统将静默地不创建附件或评论。

检索附件和评论

所有关系都由特性处理,所以您可以只需调用

$model = new MyModel();

foreach ($model->attachments as $attachment)
{
    // ...
}

foreach ($model->comments as $comment)
{
    // ...
}

显示评论和附件


许可证

MIT许可证(MIT)。有关更多信息,请参阅 许可证文件