PHP code example of r94ever / laravel-media

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

    

r94ever / laravel-media example snippets


$file = $request->file('file');

// Default usage
$media = MediaUploader::fromFile($file)->upload();

// Custom usage
$media = MediaUploader::fromFile($file)
    ->useFileName('custom-file-name.jpeg')
    ->useName('Custom media name')
    ->upload();

class Post extends Model
{
    use HasMedia;
}

$post = Post::first();

// To the default group
$post->attachMedia($media);

// To a custom group
$post->attachMedia($media, 'custom-group');

// Detach all the media
$post->detachMedia();

// Detach the specified media
$post->detachMedia($media);

// Detach all the media in a group
$post->clearMediaGroup('your-group');

Media::first()->delete();

// All media in the default group
$post->getMedia();

// All media in a custom group
$post->getMedia('custom-group');

// First media item in the default group 
$post->getFirstMedia();

// First media item in a custom group
$post->getFirstMedia('custom-group');

// Url of the first media item in the default group
$post->getFirstMediaUrl();

// Url of the first media item in a custom group
$post->getFirstMediaUrl('custom-group');

use Intervention\Image\Image;
use Optix\Media\Facades\Conversion;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Conversion::register('thumb', function (Image $image) {
            return $image->fit(64, 64);
        });
    }
}

class Post extends Model
{
    use HasMedia;
    
    public function registerMediaGroups()
    {
        $this->addMediaGroup('gallery')
             ->performConversions('thumb');
    }
}

// The thumbnail of the first image in the gallery group
$post->getFirstMediaUrl('gallery', 'thumb');
bash
php artisan vendor:publish --provider="Optix\Media\MediaServiceProvider"