Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How can I integrate MediaMan with my existing Laravel project


How can I integrate MediaMan with my existing Laravel project


To integrate MediaMan with your existing Laravel project, follow these steps:

Step 1: Installation

1. Install MediaMan via Composer:

bash
   composer require farhanshares/laravel-mediaman
   

Step 2: Configuration

1. Publish Configuration Files:

bash
   php artisan mediaman:publish-config
   php artisan mediaman:publish-migration
   

2. Link Storage:

bash
   php artisan storage:link
   

Step 3: Run Migrations

1. Run the Migration:

bash
   php artisan migrate
   

Step 4: Use MediaMan in Your Models

1. Add the `HasMedia` Trait:

php
   use Illuminate\Database\Eloquent\Model;
   use FarhanShares\MediaMan\Traits\HasMedia;

   class Post extends Model {
       use HasMedia;
   }
   

Step 5: Upload and Manage Media

1. Upload Media:

php
   $file = $request->file('file');
   $media = MediaUploader::source($file)->upload();
   

2. Attach Media to a Model:

php
   $post = Post::find(1);
   $post->attachMedia($media, 'featured-image-channel');
   

Step 6: Retrieve and Manage Media

1. Retrieve Media:

php
   $post = Post::find(1);
   $media = $post->getMedia();
   

2. Detach Media:

php
   $post->detachMedia($media);
   

Step 7: Customize MediaMan

1. Customize MediaMan Configuration:

php
   // config/mediaman.php
   'disk' => 'media',
   

2. Customize Media Model:

php
   // app/Models/Media.php
   use Illuminate\Database\Eloquent\Model;
   use FarhanShares\MediaMan\Models\Media;

   class Media extends Model {
       // Customize Media Model
   }
   

Step 8: Test and Use

1. Test Your Application:

php
   // Ensure all media operations work as expected
   

2. Use MediaMan in Your Application:

php
   // Use MediaMan in your application's media management
   

By following these steps, you can integrate MediaMan into your existing Laravel project and manage media effectively.

Citations:
[1] https://github.com/FarhanShares/laravel-mediaman
[2] https://farhanshares.com/posts/laravel-media-library-package
[3] https://github.com/talvbansal/media-manager-laravel-example
[4] https://laracasts.com/discuss/channels/laravel/integrating-maven-project-into-laravel
[5] https://laravel-news.com/laravel-media-package