PHP code example of monstrex / media-storage

1. Go to this page and download the library: Download monstrex/media-storage 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/ */

    

monstrex / media-storage example snippets


$user1 = User::find(1);
// Add file from request and bind the file to model using collection name (string)
$collection = Media::add(request()->file('myfile'))->model($user1)->collection('new-gallery')->create();
// Add file from path and disk
$collection = Media::add('images/1.jpg', 'local')->collection('local-file')->create();
// Add file using Collection ID (int)
$collection = Media::model($model)->add($files)->collection(5403)->create();
// Add file with custom properties
$collection = Media::add($file)->props(['title' => 'Novus news','alt' => 'Image #2'])->collection('images')->create();
// Add files using noname collection. You can get collection ID from returned collection - collection_id field. 
$collection = Media::add($files)->create();
// Will save original source file as is
$collection = Media::add('images/1.jpg', 'local')->preserveOriginal()->create();
// Will replace target file if exist. 
$collection = Media::add($file)->replaceFile()->create();
// Will replace target file name using language table from config file
$collection = Media::add($file)->transliterate('ru')->collection('cyrillic-gallery')->create();

// Get path by record ID
$path = Media::find(1)->path(); 
// Get path by MEDIA ID
$path = Media::id(3)->path();   
// Get relative URL
$url = Media::id(3)->url();
// Get full URL
$url = Media::id(3)->fullUrl();
// Generate conversion if not exist and return URL     
$url = Media::id(3)->crop(250,300)->url(); 
// Get collection by collection ID 
$collection = Media::collection(5403)->get();
// Get collection by collection Name
$collection = Media::model($user1)->collection('new-gallery')->get();
// Get All media entries
$collection = Media::all();
// Get all properties
Media::id(3)->props();
// Get certain propery
Media::id(3)->prop('title');
Media::id(3)->prop('setting.width');

// Set properties
Media::id(3)->props(['title' => 'New title'])->save();
// Set one property
Media::id(3)->prop('settings.width', 500)->save();
// Set order
$collection = Media::collection('new-gallery')->get();
$collection[0]->order(1);
$collection[1]->order(2);
$collection[2]->order(3); 
Media::save($collection);

// Delete one media entry
Media::id(3)->delete();
// Delete whole collection
Media::collection(5403)->delete();
// Delete collection binded to model
Media::model($user1)->collection('new-gallery')->delete()
// Delete All media entries
Media::deleteAll();
 bash
$ php artisan vendor:publish --provider="MonstreX\MediaStorage\MediaStorageServiceProvider" --tag="migrations"
$ php artisan migrate