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.
Layouts
Selected attachments are shown as grid cards by default. Use layout() to change this:
use VanOns\FilamentAttachmentLibrary\Enums\AttachmentFieldLayout;
AttachmentField::make('featured_image')->layout(AttachmentFieldLayout::INPUT);AttachmentFieldLayout::GRID(default): cards with a large preview.AttachmentFieldLayout::LIST: horizontal rows with a small preview.compact()is a shortcut for this layout.AttachmentFieldLayout::INPUT: rows that look like a text input, showing a thumbnail or icon, the name and the file type. Click a row to pick or change the file; there is no separate "Choose files" button. Withmultiple(), an extra row adds files untilmaxFiles()is reached. This layout fits well in narrow places, such as table repeaters.
Videos
When editing a video in the library, you can set a poster image and add caption tracks (.vtt, or .srt, which is
converted to .vtt). With ffmpeg installed on the server, the dimensions, duration and a first-frame poster are stored
automatically on upload, and a "Use first frame" button is available. See the
Laravel Attachment Library video docs
for rendering videos on the frontend.
