Laravel Media Uploader
Published on by Paul Redmond
The Laravel Media Uploader package by Ahmed Fathy uploads files using Spatie’s media library package before saving a model. You can then attach uploaded media to the model in a controller:
class BlogController extends Controller{ public function store(Request $request) { $blog = Blog::create($request->all()); $blog->addAllMediaFromTokens(); return back(); }}
According to the readme, media processing happens in the following ways:
- All videos will be converted to mp4.
- All audios will be converted to mp3.
- All images width & height & ratio will be saved as custom property.
- All videos & audios duration will be saved as a custom property.
You can also attach media more specifically:
// specified collection name$blog->addAllMediaFromTokens([], 'pictures'); // specified tokens$blog->addAllMediaFromTokens($request->input('tokens', []), 'pictures');
On the frontend, this package is stitched together with a companion Vue component (laravel-file-uploader):
Check out the component usage doc for details on the frontend component; a basic example might look like the following:
<file-uploader :max="1" collection="avatars" :tokens="{{ json_encode(old('media', [])) }}" label="Upload Avatar" notes="Supported types: jpeg, png, jpg, gif" accept="image/jpeg,image/png,image/jpg,image/gif"></file-uploader>
The package also includes some REST API endpoints to access uploaded files, display recently uploaded files, and show deleted media files.
You can learn more about this package, get full installation instructions, and view the source code on GitHub at ahmed-aliraqi/laravel-media-uploader.