Laravel Media Package

Published on by

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:
Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech

The latest

View all →
LaraOwl: Self-Hosted Monitoring for Laravel Applications image

LaraOwl: Self-Hosted Monitoring for Laravel Applications

Read article
Filament Storage Monitor: Track Disk Usage From Your Filament Dashboard image

Filament Storage Monitor: Track Disk Usage From Your Filament Dashboard

Read article
Subscriptionify: Feature-Based Subscription Management for Laravel image

Subscriptionify: Feature-Based Subscription Management for Laravel

Read article
Toolkit: Reusable AI Tools for the Laravel AI SDK image

Toolkit: Reusable AI Tools for the Laravel AI SDK

Read article
Laracon US 2026 Reveals Its Full Speaker Lineup image

Laracon US 2026 Reveals Its Full Speaker Lineup

Read article
The State of PHP 2026 Survey Is Now Open image

The State of PHP 2026 Survey Is Now Open

Read article