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.
Installation:
composer require van-ons/filament-attachment-library:^2.0
php artisan filament-attachment-library:install
Follow the CLI prompts to set up the custom Filament theme and register it in your panel provider.
Configure Storage Disk (optional):
Add to .env:
ATTACHMENTS_DISK=s3 # or any other disk name
Register the Plugin:
In your PanelProvider:
->plugin(FilamentAttachmentLibrary::make()->navigationGroup('Files'))
First Use Case:
Add HasAttachments trait to your model:
use VanOns\LaravelAttachmentLibrary\Concerns\HasAttachments;
class Product extends Model {
use HasAttachments;
}
Add the AttachmentField to your Filament resource form:
AttachmentField::make('gallery')->relationship()->collection('product_images')
AttachmentField::make('field_name') for storing a single attachment in a model column.AttachmentField::make('featured_image')
->required()
->image()
->imagePreviewHeight('200px')
AttachmentField::make('field_name')->relationship() for storing multiple attachments via the attachments relationship.AttachmentField::make('gallery')->relationship()->collection('product_gallery')
public function gallery(): MorphToMany {
return $this->attachmentCollection('product_gallery');
}
<x-laravel-attachment-library-image :src="$product->gallery->first()" />
glide.php:
'default' => [
'width' => 800,
'height' => 600,
'fit' => 'crop',
],
config/attachment-library.php:
'max_file_size' => '10MB', // Default: 5MB
'allowed_mime_types' => ['image/*', 'application/pdf'],
'disk' => env('ATTACHMENTS_DISK', 'public'),
use VanOns\FilamentAttachmentLibrary\Columns\AttachmentColumn;
AttachmentColumn::make('gallery')
->collection('product_gallery')
->image()
->imagePreviewHeight('50px')
public static function getBulkActions(): array {
return [
Actions\DeleteAllAttachments::make(),
];
}
TailwindCSS Styling:
tailwind.config.js:
content: [
'./vendor/van-ons/filament-attachment-library/resources/**/*.blade.php',
]
php artisan make:filament-theme [PANEL_NAME] and update tailwind.config.js.Disk Conflicts:
public) for attachments can cause conflicts with other files.attachments) and set ATTACHMENTS_DISK in .env.Glide Configuration:
glide package is not installed or misconfigured.spatie/laravel-medialibrary and intervention/image (or spatie/image-optimizer) are installed.Relationship Caching:
with() sparingly:
Product::with(['gallery' => function($query) {
$query->limit(3); // Limit attachments per model
}])->get();
File Validation:
image/jpeg vs. image/pjpeg).allowed_mime_types in config/attachment-library.php:
'allowed_mime_types' => ['image/*', 'application/pdf', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'],
Attachment Not Showing?
HasAttachments trait is applied to the model.Upload Fails Silently
Symfony\Component\HttpFoundation\File\UploadedFile errors.php artisan storage:link
Glide Processing Errors
php artisan glide:clear
glide.php for correct disk and path configurations.Custom Attachment Models:
Attachment model to add custom fields (e.g., alt_text, metadata):
php artisan make:model AttachmentExtension --extend=VanOns\LaravelAttachmentLibrary\Models\Attachment
Custom Storage Logic:
AttachmentServiceProvider to change upload behavior:
use VanOns\LaravelAttachmentLibrary\AttachmentServiceProvider as BaseProvider;
class CustomAttachmentServiceProvider extends BaseProvider {
public function register() {
$this->app->singleton(\VanOns\LaravelAttachmentLibrary\Contracts\AttachmentUploader::class, function ($app) {
return new CustomUploader();
});
}
}
Custom UI Components:
AttachmentField or AttachmentColumn by publishing views:
php artisan vendor:publish --tag=filament-attachment-library-views
resources/views/vendor/filament-attachment-library/... to customize the UI.Event Listeners:
AttachmentCreated, AttachmentDeleted) to trigger actions:
use VanOns\LaravelAttachmentLibrary\Events\AttachmentCreated;
event(new AttachmentCreated($attachment));
EventServiceProvider:
protected $listen = [
AttachmentCreated::class => [
\App\Listeners\LogAttachmentCreation::class,
],
];
Bulk Operations:
use VanOns\FilamentAttachmentLibrary\Actions\BulkAction;
BulkAction::make('compress-images')
->action(function (array $records) {
foreach ($records as $record) {
$record->gallery()->update(['width' => 800, 'height' => 600]);
}
}),
How can I help you explore Laravel packages today?