Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Attachment Library Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Configuring your 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;
    
    // ...
}

Attach attachments

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']);

Detach attachments

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);

Retrieve attachments

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();

Ordering

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.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours