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

Filament Attachment Library Laravel Package

van-ons/filament-attachment-library

Filament Attachment Library adds a simple attachments manager to your Filament panel: upload files, browse and select existing attachments, and store them in a central library. Includes installer command, migrations/assets, and Tailwind-ready templates.

View on GitHub
Deep Wiki
Context7

Handling attachments

The attachment field can be used in two ways: either to store the attachments in a specific column of your model, or to store the attachments in the attachments relationship by using the HasAttachments trait and the relationship() method.

(Optional) Add the HasAttachments trait to your desired model:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use VanOns\LaravelAttachmentLibrary\Concerns\HasAttachments;

class ModelName extends Model
{
    use HasAttachments;

    // ...
}

This will add the attachments() relationship which links one or more attachments to your model. By default the attachments will be stored in a collection that matches the name of the field (e.g. gallery). You can provide a different collection by calling collection('collection_name').

Then, in your form schema, add the AttachmentField:

namespace App\Filament\Resources;

use Filament\Forms;
use Filament\Forms\Form;
use VanOns\FilamentAttachmentLibrary\Forms\Components\AttachmentField;

public static function form(Form $form): Form
{
    return $form
        ->schema([
            // ...
            // If you want to store the attachments in a column
            AttachmentField::make('featured_image'),
            // Or if you want to store attachments in the attachments relationship with a specific collection name
            AttachmentField::make('gallery')->relationship(),
            // Or if you want the collection name to be different from the field name
            AttachmentField::make('gallery')->relationship()->collection('product_gallery'),
        ]);
}

(Optional) When using the relationship() method, you can filter the attachments by collection name. To make this 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');
    }
}

Finally, at the front end, the laravel-attachment-library-image Blade component can be used to display attachments as image. Glide is used to scale the image up or down. The src argument may be an Attachment instance, or the id as string/integer.

<x-laravel-attachment-library-image :src="$image" />

For more information refer to the Laravel Attachment Library documentation.

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.
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky