| Install | |
|---|---|
composer require van-ons/filament-attachment-library |
|
| Latest Version: | v2.2.6 |
| PHP: | ^8.2 |

Filament package for easy attachment uploading and browsing.
For certain Filament versions, changes have to be made that render the package backwards incompatible with the previous version. Please see the table below to determine which version you need.
| Version | Filament |
|---|---|
| v2 (current) | >=4.0 | >=5.0 |
| v1 | <4.0 |
Please note: the main branch will always be the latest major version.
The Filament Attachment Library can be installed using Composer by running the following command:
composer require van-ons/filament-attachment-library:^2.0
An installation command is available that ensures that the migrations and assets are installed:
php artisan filament-attachment-library:install
The templates in this package use TailwindCSS. To ensure the styling is rendered correctly, a custom Filament
theme must be set up, and the tailwind.config.js file should be extended.
Create the custom Filament theme and follow the instructions in the terminal to set it up:
php artisan make:filament-theme [PANEL_NAME]
Add the following to the generated theme.css file:
// resources/css/filament/[PANEL_NAME]/theme.css
@source '../../../../vendor/van-ons/filament-attachment-library/resources/**/*.blade.php'
[!NOTE] Make sure to follow the instructions in the terminal to register your custom Filament theme in the admin panel.
If your project is using Vite, you may have to register the custom theme as follows:
->viteTheme('resources/css/filament/[PANEL_NAME]/theme.css', 'build')
By default, this package uses the public disk defined in filesystems.php. This can be overridden by adding the following
to the project's .env file:
ATTACHMENTS_DISK=disk_name_here
[!NOTE] It is advised to use a disk without any other files. This prevents file conflicts.
The glide.php and attachment-library.php files contain more configuration options.
Register the plugin in the desired Filament panel:
namespace App\Providers\Filament;
use VanOns\FilamentAttachmentLibrary\FilamentAttachmentLibrary;
class ExamplePanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
->plugin(
FilamentAttachmentLibrary::make()->navigationGroup('Files')
);
}
}
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.
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.
Please see the documentation for detailed information about installation and usage.
Please see contributing for more information about how you can contribute.
Please see changelog for more information about what has changed recently.
Please see upgrading for more information about how to upgrade.
Please see security for more information about how we deal with security.
We would like to thank the following contributors for their contributions to this project:
The scripts and documentation in this project are released under the MIT License.