andach / laravel-attachments-comments
This is a Laravel package to allow you to add comments and attachments to models.
Requires
- php: ^8.1
- andach/laravel-primary-key-uuid: ^1.0
- illuminate/contracts: ^10.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.8
- orchestra/testbench: ^8.8
- phpunit/phpunit: ^10.0
README
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.