Skip to content

waadmawlood/media

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Logo

πŸ”₯ Media Files Package

A Package to save your files Many Disks , Many Diroctories By Same Model

❀️ Authors

⚠️ Mini Requirement

* version >= 2.0.0

  • PHP >= 8.0.0
  • Laravel 8 , 9 , 10

Β 

* version < 2.0.0

  • PHP 7.4
  • Laravel 7 , 8

πŸ’― Installation

To install

composer require waad/media

first : publish the config with command:

php artisan vendor:publish --provider="Waad\Media\MediaServiceProvider" --tag="media-config"

configration from config/media.php sure from uuid, shortcut in config media

⚠️ clear cache important, before publish migrations

php artisan optimize

Second : publish the migrations with command:

php artisan vendor:publish --provider="Waad\Media\MediaServiceProvider" --tag="media-migrations"

You can migration

php artisan migrate

You can make a link shortcut by disk from config.media.shortcut array of disks

'shortcut' => [
        'public' => 'media',
        // disk => shortcut name
    ],
php artisan media:link

🧰 Usage / Example

In Model

<?php

namespace App\Models;

use Waad\Media\Traits\HasOneMedia;
// or
use Waad\Media\Traits\HasManyMedia;

class Post extends Model
{
    use HasOneMedia;       <<------ return one last record of media 
    // or
    use HasManyMedia;      <<------ return list of media


    // $media_disk
    // $media_directory 
    // if not define will get default `disk,directory`  in `config/media.php`
    public $media_disk = 'public';
    public $media_directory = 'posts/images';
    ......

Β 

You Can get media

$post->media;

Β 

  • Upload Files eg. Use in controller store method to add One or Many Files
$post = Post::create([
  ...........
]);

$files = $request->file('image'); // one image
$files = $request->file('images'); // many images

// version < 2 
// will return array of file names
$media = $post->addMedia($files); 
$media = $post->addMedia($files, $index = 1, $label = 'cover'); 


// ***************************************************


// version >= 2 
// will return Media model or array of Media model by on Relationship
$media = $post->addMedia($files)->upload();
$media = $post->addMedia($files)->label('cover')->index(3)->upload();
$media = $post->addMedia($files)->disk('public')->directory('posts/video')->label('cover')->index(3)->upload();


return $media;

Β 

  • Sync Files eg. Use in controller update method to add One or Many Files
$post = Post::find(1);
$post->update([
  ...........
]);

$files = $request->file('image'); // one image
$files = $request->file('images'); // many images

// version < 2 
// will return array of file names
$media = $post->syncMedia($files);
$media = $post->syncMedia($files, $index = 2);


// ***************************************************


// version >= 2 
// will return Media model or array of Media model by on Relationship
$media = $post->syncMedia($files)->sync();
$media = $post->syncMedia($files, $ids = [1,3])->sync(); // delete only these $ids and upload new files
$media = $post->syncMedia($files)->label('cover')->index(3)->sync();
$media = $post->syncMedia($files)->disk('public')->directory('posts/video')->label('cover')->index(3)->sync();

return $media;
  • Delete Files eg. Use in controller destroy method to delete all or specific ids
$post = Post::find(1);

// version < 2 
// will return array of file names
$media = $post->deleteMedia($files);
$media = $post->deleteMedia($files, $index = 2);


// ***************************************************


// version >= 2 
// will return bool or array of bool or null by on Relationship
$media = $post->deleteMedia()->delete();
$media = $post->deleteMedia($medias_model)->delete();
$media = $post->deleteMedia([1,3])->delete(); // delete only these ids

$lastMedia = $post->media->last(); // return Collection Media Model 
$media = $post->deleteMedia($lastMedia)->delete(); // delete only this media

$media2 = $post->mediaById(8);
$media = $post->deleteMedia($media2)->delete(); 

$mediaList = $post->mediaByMimeType('image/png');
$media = $post->deleteMedia($mediaList)->delete(); 

$post->delete();

Β 

  • Other Helper Only version >= 2
// get sum files size of post object (bytes)
$post->mediaTotalSize();

//**********************************************

// get count media of post object
$post->mediaTotalCount();

// get count media with soft delete of post object
$post->mediaTotalCount($withTrashed = true);  

//**********************************************

// get media by id of post object
$post->mediaById(17);

// get media with soft delete by id of post object
$post->mediaById(17, $withTrashed = true);    

//**********************************************

// get media by mime_type of post object
$post->mediaByMimeType('image/png');

// get media with soft delete by mime_type of post object
$post->mediaByMimeType('image/png', $withTrashed = true);

//**********************************************

// get media by approved boolean of post object
$post->mediaApproved();      // default true
$post->mediaApproved(false);

// get media with soft delete by approved boolean of post object
$post->mediaApproved(false, $withTrashed = true);

Β 

  • You can update approved all media of object
$post->media->approve();   // put approved = true

$post->media->disApprove();   // put approved = false

Β 

  • You can get the user to upload that media
// if was HasOneMedia
1 - optional($post->media)->user;
2 - Post::with('media.user')->find(1);

//**********************************************

// if was HasManyMedia
1 - Post::with('media.user')->get();
2 - $post->media->load('user');

πŸŽ€ Scope

You can get only approved equal true

$post->media->approved();  // approved = true

πŸ” Permanently delete files

Determine delete_file_after_day from config/media.php must be integer

⭕️ Add Command to crontab of project to implemented automatically

in app/Console/Kernel.php add this:

protected function schedule(Schedule $schedule)
    {
        // .....................

        $schedule->command('media:prune')->daily();
    }

⭕️ implemented manually

php artisan media:prune

🎯 License

MIT License

About

Media managment package to save your files in one place, without worrying about how to upload and delete files

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages