Laravel Media Handler package.
composer require aammui/laravel-media
Laravel Compatibility
Laravel Version | Role Permission Version | Installation |
---|---|---|
10.x | composer require aammui/laravel-media:3.0.0 |
|
9.x | 2.0.0 | composer require aammui/laravel-media:2.0.0 |
8.x, 7.x, 6.x | 1.0.0 | composer require aammui/laravel-media:1.0.0 |
php artisan vendor:publish --provider="Aammui\LaravelMedia\LaravelMediaServiceProvider"
php artisan migrate
use Aammui\LaravelMedia\Traits\HasMedia;
use Illuminate\Database\Eloquent\Model;
class Document extends Model
{
use HasMedia;
}
$user->addMedia(request()->file);
$user->toCollection('profilePicture')
->addMedia(request()->file);
$user->toCollection('profilePicture')
->toDisk('public')
->addMedia(request()->file);
$user->toCollection('profilePicture')
->toDisk('public')
->addMedia(request()->file);
This doesnot download media into your storage.
It just add url to the database and when your application serve,
media will load from remote url.
$user->addMediaFromUrl('http://example.com/image.jpeg');
$user->setDownloadTrue()
->addMediaFromUrl('http://example.com/image.jpeg');
$user->getMedia();
$user->fromCollection('profilePicture')
->fromDisk('local')
->getMedia();
The standard responsive image size can be defined into the configuration file. The original image will be resized into the different sizes defined in configuration.
use Aammui\LaravelMedia\Enum\Responsive;
'responsive'=>[
'responsive' => [
Responsive::SM => [
'w' => 50,
'h' => 50,
],
Responsive::MD => [
'w' => 150,
'h' => 150,
],
Responsive::LG => [
'w' => 600, // Can define either height or width only.
],
],
];
use Aammui\LaravelMedia\Facades\Media;
use Aammui\LaravelMedia\Enum\Responsive;
Media::ofSize([Responsive::SM,Responsive::MD])
->addMedia(request()->file);
'optimize'=>true
Suppose you want to add file which doesnot belong to any model, then you can create standalone media. Use case could be you are storing images in description of some products in ecommerce shop, Where you simply store images in database before the product form is submitted via api, which returns public image url.
use Aammui\LaravelMedia\Facades\Media; // Use Media facade instead
Media::addMedia(request()->file); // returns Media Object
Media::toCollection('profilePicture')
->addMedia(request()->file); // returns Media Object while storing grouping files in profilepicture
Media::toCollection('profilePicture')
->toStorage('s3')
->addMedia(request()->file); // returns Media Object while storing files in s3