Upload multiple files
Allows you to upload multiple files in one upload form or using drag and drop functionality.
In my example, I’m working with a Product model, replace the Product with your own.
Make sure your model implements the HasMedia interface and also make sure to import the 4 classes:
use Spatie\MediaLibrary\HasMedia;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Spatie\MediaLibrary\InteractsWithMedia;
use App\Models\Concerns\AttachmentConcern;
And finally, add the InteractsWithMedia
and AttachmentConcern
trait to the class:
use HasFactory, InteractsWithMedia, AttachmentConcern;
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use App\Models\Concerns\AttachmentConcern;
class Product extends Model implements HasMedia
{
use HasFactory;
use InteractsWithMedia;
use AttachmentConcern;
...
}
Finally, add the x-admin.media
component to the view.
In resources/views/admin/products/fields.blade.php
add the following:
@if (isset($product))
<x-admin.media :attachable=$product title="Media" />
@endif
Which $product
is the record that you want to connect to uploaded files
Go test it out!
Javascript file
resources/js/admin/bulk_upload.js