themightysapien/medialibrary

Media Library on top of spatie media package. Helps to resuse uploaded media.

1.3.12 2023-06-07 15:23 UTC

This package is auto-updated.

Last update: 2024-05-07 17:16:49 UTC


README

Latest Version on Packagist Total Downloads GitHub Actions

This packages adds a reusable library functionality on top of Spatie Media Library package.

Features

  • Add media to library
  • Use media from library
  • Add media through library
  • Clear library

Installation

You can install the package via composer:

composer require themightysapien/medialibrary
# publish config and migrations
php artisan vendor:publish --provider="Themightysapien\MediaLibrary\MediaLibraryServiceProvider" --tag="config" --tag="migrations"
# Check config files for any modifications then run
php artisan migrate

Prepare Your Model

Just add InteractsWithMediaLibrary trait on top of spatie's model setup.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Themightysapien\MediaLibrary\Traits\InteractsWithMediaLibrary;

class YourModel extends Model implements HasMedia
{
    use InteractsWithMedia;
    use InteractsWithMediaLibrary;
}

Usage

Add Media to library

use Themightysapien\MediaLibrary\Facades\MediaLibrary;

$media = MediaLibrary::open($user_id)->addMedia($file);

Associate file for model through library.

This will first add the file to library and then associate media to the model.

$model->addMediaThroughLibrary($file, $user_id)
// chain through any spatie's File Adder functions
->toMediaCollection();

Associate library media for model.

$model->addMediaFromLibrary($media);
// chain through any spatie's File Adder functions
->toMediaCollection();

Clear Library

use Themightysapien\MediaLibrary\Facades\MediaLibrary;

MediaLibrary::clear($user_id);

Get library media collection

use Themightysapien\MediaLibrary\Facades\MediaLibrary;

// All Media
MediaLibrary::allMedia($user_id);

// Builder
MediaLibrary::query($user_id)->limit(5)->lastest()->get();

Get library media collection through api

$response = $this->json('GET', '{PREFIX_FROM_CONFIG}/tsmedialibrary', [
    'name' => 'document', // matches file and name with document
    'type' => 'pdf', //matches mime type with pdf
    'sort_by' => 'created_at',
    'sort_type' => 'DESC',
    'per_page' => 10 // default set on config
]);

['items' => $mediaCollection, 'pagination' => $pagination] = $response;

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email themightysapien@gmail.com instead of using the issue tracker.

Credits

License

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

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.