News

Laravel Media Package

Published
Laravel Media Package image

Laravel Media is a package by Jack Robertson for attaching files to eloquent models:

An easy solution to attach files to your eloquent models, with image manipulation built in!

This package works by handling file uploads via the package’s MediaUploader class. By default, the MediaUploader class will use the disk specified in the media configuration provided by the package. It saves the file as a sanitized version and creates a media record in the database.

Here’s a few examples from the readme of basic and customized usage of the MediaUploader class:

$file = $request->file('file');
 
// Default usage
$media = MediaUploader::fromFile($file)->upload();
 
// Custom usage
$media = MediaUploader::fromFile($file)
->useFileName('custom-file-name.jpeg')
->useName('Custom media name')
->upload();

Here’s an example from the readme of associating media with a model:

use Optix\Media\HasMedia;
 
class Post extends Model
{
use HasMedia;
}
 
 
$post = Post::first();
 
// To the default group
$post->attachMedia($media);
 
// To a custom group
$post->attachMedia($media, 'custom-group');

Further, you can register media conversions in a service provider and then perform those conversions on a model:

// In a service provider's boot() method...
Conversion::register('thumb', function (Image $image) {
return $image->fit(64, 64);
});
 
// Perform a conversion on a model
class Post extends Model
{
use HasMedia;
 
public function registerMediaGroups()
{
$this->addMediaGroup('gallery')
->performConversions('thumb');
}
}

You can learn more about this package, get full installation instructions, and view the source code on GitHub at optixsolutions/laravel-media.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Filed in

Sponsored

acquaintsoft logo
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech

The latest

View all →
Agent Run Observability in Laravel AI SDK 0.11 image

Agent Run Observability in Laravel AI SDK 0.11

Read article
Debounced Queued Event Listeners in Laravel image

Debounced Queued Event Listeners in Laravel

Read article
Statamic Mailables Viewer Previews Laravel Emails in the Control Panel image

Statamic Mailables Viewer Previews Laravel Emails in the Control Panel

Read article
Laravel Tackle: Run an AI Coding Agent in Your Laravel App image

Laravel Tackle: Run an AI Coding Agent in Your Laravel App

Read article
Queue::forward(): Reroute Laravel Queues in One Place image

Queue::forward(): Reroute Laravel Queues in One Place

Read article
Laravel Read-Through Filesystem: Lazy Storage Migration image

Laravel Read-Through Filesystem: Lazy Storage Migration

Read article