van-ons/laravel-attachment-library
Attach files to Laravel Eloquent models with a simple HasAttachments trait and Attachment model. Includes installer command for migrations/assets and an attachments relationship to link existing uploads to any model.
Accepting attachments for your model requires the use of the trait: HasAttachments. Your model should look like the following:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use VanOns\LaravelAttachmentLibrary\Concerns\HasAttachments;
class ModelName extends Model
{
use HasAttachments;
// ...
}
You can attach existing attachments to your object in the following way:
// Retrieve attachment.
$attachment = \VanOns\LaravelAttachmentLibrary\Models\Attachment::find($attachmentId);
// Retrieve your model.
$myModel = ModelName::find($modelId);
// Link attachment to your model.
$myModel->attachments()->attach($attachment);
// Or, to add the attachment to a specific collection:
$myModel->attachments()->attach($attachment, ['collection' => 'collection_name_here']);
You can detach existing attachments from your object in the following way:
// Retrieve attachment.
$attachment = \VanOns\LaravelAttachmentLibrary\Models\Attachment::find($attachmentId);
// Retrieve your model.
$myModel = ModelName::find($modelId);
// Detach attachment from your model.
$myModel->attachments()->detach($attachment);
You can retrieve the attachments for your model in the following way:
// Retrieve all attachments
$model->attachments()->get();
// Retrieve attachments for a specific collection
$model->attachments()->wherePivot('collection', 'collection_name_here')->get();
To make querying attachments in a specific collection easier, you can add a separate relationship method to your model:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use VanOns\LaravelAttachmentLibrary\Concerns\HasAttachments;
class ModelName extends Model
{
use HasAttachments;
public function gallery(): MorphToMany
{
return $this->attachmentCollection('gallery');
}
}
This allows you to retrieve the attachments in the gallery collection like so:
$model->gallery()->get();
You can order the attachments for your model by providing an order value when attaching an attachment:
$myModel->attachments()->attach($attachment, ['collection' => 'collection_name_here', 'order' => 1]);
The relationship orders the attachments by the order value in ascending order. If no order value is provided, it defaults to 0. This means that attachments without an order value will be ordered before attachments with an order value.
To retrieve attachments in a specific order from an array of ID's you can use the findOrdered method:
$attachmentIds = [3, 1, 2];
$attachments = Attachment::findOrdered($attachmentIds);
This is useful when you are not storing the attachments in a relationship with an order value, but you still want to retrieve the attachments in a specific order.
How can I help you explore Laravel packages today?