Looking to hire Laravel developers? Try LaraJobs
This package is not available.

laravel-media maintained by bakkali

Description
Simple polymorphic media library for Laravel
Last update
2026/01/08 10:41 (dev-main)
License
Downloads
208

Comments
comments powered by Disqus

📦 Laravel Media

Latest Version on Packagist Total Downloads License

Simple, lightweight polymorphic media library for Laravel.
Attach, retrieve, and manage media files (images, documents, etc.) on any Eloquent model.

🛠 Created by Yusuf Bakkali

✨ Features

  • Polymorphic relation: attach media to any Eloquent model
  • Custom collection names (e.g., 'gallery', 'cover')
  • JSON fields for flexible metadata
  • Easily add / get / remove media files
  • Designed to be simple & extendable

⚡ Installation

1️⃣ Require the package

 composer require bakkali/laravel-media:dev-main

2️⃣ Publish migration & migrate


 php artisan vendor:publish --tag=media-migrations
 php artisan migrate

🧩 Usage

use Bakkali\Media\Traits\HasMedia;

class Post extends Model
{
    use HasMedia;

    // your code...
}

📥 Add media to a model


$post = Post::find(1);

// Add a media file to 'images' collection
$post->addMedia('/path/to/file.jpg', 'images');

📂 Get media by collection


$images = $post->getMedia('images');

foreach ($images as $media) {
    echo $media->file_name;
    echo $media->disk;
}

✏ Update media attributes


$post->updateMedia($mediaId, [
    'name' => 'New name'
]);

🧹 Soft delete media (alias)


$post->destroyMedia($mediaId);

❌ Permanently delete media


$post->forceDeleteMedia($mediaId);

🛠 Development & Updates


- composer update bakkali/laravel-media:dev-main