My Blog Posts

Laravel Media Package

Laravel Media Package


An easy solution to add files to your better models with image manipulation you're in!

This package allows you to MediaUploaderby performing through the class. By default, the MediaUploader class uses the disk specified in the media configuration provided by the package. Saves the file as a cleaned version and creates a media record in the database.

Here are some examples of the basic and customized use of the MediaUploader class:

$file = $request->net('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 of how you associate 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');

You can also save media conversions to a service provider and then perform these conversions in 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 all the setup instructions, and find the source code at optixsolutions / laravel-media on GitHub.



Share this article


Comments (0)

Comment