PHP code example of themightysapien / medialibrary

1. Go to this page and download the library: Download themightysapien/medialibrary library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

themightysapien / medialibrary example snippets


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;
}

use Themightysapien\MediaLibrary\Facades\MediaLibrary;

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

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

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

use Themightysapien\MediaLibrary\Facades\MediaLibrary;

MediaLibrary::clear($user_id);

use Themightysapien\MediaLibrary\Facades\MediaLibrary;

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

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

$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;