- Laravel Media Gallery
The Laravel Media Gallery simplifies media and media file management in your Laravel project. This README provides installation instructions, usage examples, and additional information.
To get started, install the package using Composer:
composer require anisaronno/laravel-media-gallery
For media library features, follow these steps:
Publish the migration, factory and seeder file:
php artisan vendor:publish --tag=media-migration
Publish the Config file:
php artisan vendor:publish --tag=media
Run Migration
Apply the migrations to set up the media storage table:
php artisan migrate
To use media storage in any model (e.g., User Blog, Product), add the HasMedia
trait:
use AnisAronno\MediaGallery\Traits\HasMedia;
use HasMedia;
For setting up seed data with relation mapping (e.g., User has Blog, Blog uses HasMedia Trait), use the following code in a seeder:
use App\Models\User;
use AnisAronno\MediaGallery\Database\Factories\MediaFactory;
User::factory(20)
->hasAttached(
MediaFactory::new()->count(5)
)
->afterCreating(function (User $user)
{
$featuredMedia = $user->media()->first();
$featuredMedia->pivot->is_featured = true;
$featuredMedia->pivot->save();
})
->create();
To retrieve media by the user, use the HasMedia
trait on the User/Team/Admin or any other model authorized to upload media:
use AnisAronno\MediaGallery\Traits\HasMedia;
use HasOwnedMedia;
$user = User::find(1); // or auth()->user();
$user->ownedMedia();
Absolutely, here's the updated text with subheadings for each section:
You can customize the authentication guard for the routes by publishing the config file and changing the 'guard' key to your desired authentication guard.
'guard' => ['auth'],
Alternatively, you can use the API middleware.
'guard' => ['auth:sanctum'],
Set view_all_media_anyone
to false
to restrict media viewing to user-uploaded images only; default is true
, allowing all media viewing.
'view_all_media_anyone' => false,
Defines canManageMediaContent
gate in AuthServiceProvider.php
allowing designated users to manage media content.
use Illuminate\Support\Facades\Gate;
Gate::define('canManageMediaContent', function (User $user) {
return in_array($user->email, [
'[email protected]',
]);
});
Set Cache Expiry time. Default value is 1440.
'cache_expiry_time' => 1440,
For storing media for a relational model (e.g., Blog), use the following methods:
- Attach:
$blog->media()->attach(array $id)
- Sync:
$blog->media()->sync(array $id)
- Delete:
$blog->media()->detach(array $id)
To work with a single or featured media, use the featuredMedia
method and set isFeatured
to true
in the second parameter:
- Attach:
$blog->featuredMedia()->attach(array $id, ['is_featured' => 1])
Note: Sync and detach are the same; use featuredMedia
instead of featuredMedia
.
You can also use helper methods for media management:
-
For Attach:
$blog->attachMedia(array $ids, $isFeatured = false)
-
For Sync:
$blog->syncMedia(array $ids, $isFeatured = false)
-
For Delete:
$blog->detachMedia(array $ids, $isFeatured = false)
To manage your media storage, you can use the following routes:
- Get all media:
api/media
(GET) - Get a single media:
api/media/{id}
(GET) - Store an media:
api/media
(POST) - Delete an media:
api/media/{id}
(DELETE) - Update an media:
api/media/update/{id}
(POST) - Batch Delete:
media/batch-delete
(POST)
To retrieve media/media from a relational model:
-
Fetch all media as an array:
$blog->media
-
Fetch the featured media only:
$blog->media
You can access media properties like URL, title, mimes, size, and type:
$media->url
$media->title
$media->mimes
$media->size
$media->type
Please follow our Contribution Guide if you'd like to contribute to this package.
This package is open-source software licensed under the MIT License.