shrestharikesh/laravel-media-manager

Easily Upload Media in laravel

dev-main 2023-07-05 01:28 UTC

This package is auto-updated.

Last update: 2024-05-05 03:13:30 UTC


README

Easily associate your Media with Eloquent Models

Getting Started

Install the package

  • composer require shrestharikesh/laravel-media-manager

Run the migration

  • php artisan migrate

Usage

Use the trait given by the package into any model:

use Shrestharikesh\LaravelMediaManager\HasMedia;

class Post extends Model
{
    use HasMedia;
    // Rest of your code
}

Associate as many media as you want to the model:

$post = Post::create($data);
$post->addMedia($request->image);

Or just one to each tag of the model:

$post->addMedia($request->image)->deletePrevious();

Easily upload media to different tags:

$post->addMedia($request->featured_image)->withTag('featured');
$post->addMedia($request->thumbnail_image)->withTag('thumbnail')->withAltText('Featured Image');

Get all the associated media easily:

$post = Post::find(1);
$post->getMedia($request->featured_image); // Returns medias from all topics
$post->getMedia($request->featured_image, 'default'); // Medias uploaded without any tag
$post->getMedia($request->featured_image, 'featured');

Or just the one:

$post->getFirstMedia($request->featured_image);
$post->getFirstMedia($request->featured_image, 'featured');

Show the media easily to your blade files:

<img src="{{$featured_post->getFirstMedia('featured')?->url}}"/>

You are about to remove your model and want to remove all associated medias?

$post = Post::find(1);
$post->deleteMedia();
$post->delete();

Or just delete medias from specific topic:

$post = Post::find(1);
$post->deleteMedia('featured');

Maybe just the one you don't need anymore:

$post->deleteSpecificMedia(1); // Pass the media id

Oh no! you've found bugs? Feel free to create an issue on GitHub, we'll fix it ASAP.

License

The MIT License (MIT). Please see License File for more information.