andach/laravel-attachments-comments

This is a Laravel package to allow you to add comments and attachments to models.

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

This package is auto-updated.

Last update: 2024-04-27 12:27:17 UTC


README

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

This is a Laravel package for adding Attachments and Comments to any model.

Installation

You can install the package via composer:

composer require andach-limited/laravel-attachments-comments

You should then publish the config and migrations with:

php artisan attachments-comments:install

Usage

Using a Trait

To add the relations to a model, simply add the MorphToAttachmentsAndComments trait to the model. If only one is needed, then just use MorphToAttachments or MorphToComments by themselves.

class MyModel extends Model
{
    use MorphToAttachmentsAndComments;

    // ...
}

Adding Attachments and/or Comments

To actually add an attachment or a comment, you can use the addAttachmentAndComment(). This accepts a single string and an optional 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>

And in the relevant controller method...

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

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

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

If handed an empty string or null attachment, the system will not create an attachment or comment silently.

Retrieving Attachments and Comments

The relations are all handled by the traits, so you can just call:

$model = new MyModel();

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

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

Displaying Comments and Attachments


License

The MIT License (MIT). Please see License File for more information.