fomvasss / laravel-medialibrary-extension
Extension for use spatie/laravel-medialibrary
Installs: 1 945
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- illuminate/support: ^10.0|^11.0
- spatie/laravel-medialibrary: ^11.0
- dev-master
- 6.x-dev
- 6.1.0
- 6.0.1
- 6.0.0
- 5.x-dev
- 5.8.0
- 5.7.0
- 5.6.0
- 5.5.0
- 5.4.0
- 5.3.0
- 5.2.0
- 5.1.0
- 5.0.1
- 5.0.0
- 4.x-dev
- 4.9.0
- 4.8.0
- 4.7.0
- 4.6.1
- 4.6.0
- 4.5.0
- 4.4.0
- 4.3.0
- 4.2.0
- 4.1.0
- 4.0.2
- 4.0.1
- 4.0.0
- 3.1.0
- 3.0.0
- 2.0.0
- 1.2.0
- 1.1.0
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-develop
- dev-fix_namespaces-1.2.0
- dev-fix_namespaces
This package is auto-updated.
Last update: 2024-10-16 13:36:47 UTC
README
Extensions to the file management functionality of package spatie/laravel-medialibrary
Installation
composer require fomvasss/laravel-medialibrary-extension
Publish spatie/laravel-medialibrary
(if this has not been done before)
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="medialibrary-migrations" php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="medialibrary-config"
Publish fomvasss/laravel-medialibrary-extension
php artisan vendor:publish --provider="Fomvasss\MediaLibraryExtension\ServiceProvider"
Run migration:
php artisan migrate
Add facade to config/app.php
to aliases
(optional):
//... 'MediaManager' => \Fomvasss\MediaLibraryExtension\Facade::class, //...
Integration in Eloquent models
Implements interface:
Fomvasss\MediaLibraryExtension\HasMedia\HasMedia
Usage in Eloquent models trait:
Fomvasss\MediaLibraryExtension\HasMedia\InteractsWithMedia
Usage
In model app/Models/Article.php
:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Spatie\MediaLibrary\Models\Media; use Fomvasss\MediaLibraryExtension\HasMedia\HasMedia; use Fomvasss\MediaLibraryExtension\HasMedia\InteractsWithMedia; class Article extends Model implements HasMedia { use InteractsWithMedia; // html-input name == media collection name, example: protected $mediaSingleCollections = ['image']; protected $mediaMultipleCollections = ['images', 'files']; /** * Optional method. */ public function customMediaConversions(Media $media = null): void { $this->addMediaCollection('main') ->singleFile(); $this->addMediaConversion('table') ->format('jpg')->quality(93) ->fit('crop', 360, 257); } }
In controller app/Http/Controllers/Article.php
:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class HomeController extends Controller { public function store(PostRequest $request) { $article = Article::create($request->validated()); $article->mediaManage($request); // Or usage Facade: \MediaLibrary::manage($article, $request); //... } public function show($id) { $article = \App\Model\Article::findOrFail($id); $url = $article->getFirstMediaUrl('image'); $image = $article->getFirstMedia('image'); $alt = $image->getCustomProperty('alt'); $images = $article->getMedia('images'); // collection // ... // Also available all methods spatie/laravel-medialibrary! } }
Examples HTML form
For upload files from collection name images
you need send form with next data:
<input type="file" name="image"> <input type="hidden" name="image_custom[15][alt]" value="This Alt image text"> <input type="file" multiple="" name="images[]">
For set sort order file from collection name images
and id = 13, 15
you need send form with next data (weight = 21, 22
):
<input type="hidden" name="images_weight[15]" value="21"> <input type="hidden" name="images_weight[13]" value="22"> <input type="hidden" name="images_custom[13][alt]" value="Some text">
For deleted file from collection name images
and id = 13, 15
you need send form with next data:
<input type="hidden" name="images_deleted[]" value="13"> <input type="hidden" name="images_deleted[]" value="15">