PHP code example of farhanshares / laravel-mediaman
1. Go to this page and download the library: Download farhanshares/laravel-mediaman 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/ */
$file = $request->file('file')
$media = MediaUploader::source($file)
->useName('Custom name')
->useFileName('custom-name.png')
->useCollection('Images')
->useDisk('media')
->useData([
'additional_data' => 'will be stored as json',
'use_associative_array' => 'to store any data you want to be with the file',
])
->upload();
// by id
$media = Media::find(1);
// by name
$media = Media::findByName('media-name');
// with collections
$media = Media::with('collections')->find(1);
'id' => int
'name' => string
'file_name' => string
'extension' => string
'type' => string
'mime_type' => string
'size' => int // in bytes
'friendly_size' => string // in human readable format
'media_uri' => string // media URI for the original file. Usage in Blade: {{ asset($media->media_uri) }}.
'media_url' => string // direct URL for the original file.
'disk' => string
'data' => array // casts as array
'created_at' => string
'updated_at' => string
'collections' => object // eloquent collection
// $media->mime_type => 'image/jpg'
$media->isOfType('image') // true
// get the media url, accepts optional '$conversionName' argument
$media->getUrl('conversion-name')
// get the path to the file on disk, accepts optional '$conversionName' argument
$media->getPath('conversion-name')
// get the directory where the media stored on disk
$media->getDirectory()
$media->disk = 's3'; // Example disk name, ensure it exists
$media->save();
$media = Media::first();
$media->delete()
Media::destroy(1);
Media::destroy([1, 2, 3]);
use Illuminate\Database\Eloquent\Model;
use FarhanShares\MediaMan\Traits\HasMedia;
class Post extends Model
{
use HasMedia;
}
$post = Post::first();
// Associate in the default channel
$post->attachMedia($media); // or 1 or [1, 2, 3] or collection of media models
// Associate in a custom channel
$post->attachMedia($media, 'featured-image');
// All media from the default channel
$post->getMedia();
// All media from the specified channel
$post->getMedia('featured-image');
$media = $post->getMedia('featured-image');
// getUrl() accepts only one optional argument: name of the conversion
// leave it empty to get the original media URL
$mediaOneUrl = $media[0]->getUrl();
// First media item from the default channel
$post->getFirstMedia();
// First media item from the specified channel
$post->getFirstMedia('featured-image');
// URL of the first media item from the default channel
$post->getFirstMediaUrl();
// URL of the first media item from the specified channel
$post->getFirstMediaUrl('featured-image');
// Detach the specified media
$post->detachMedia($media); // or 1 or [1, 2, 3] or collection of media models
// Detach all media from all channels
$post->detachMedia();
// Detach all media of the default channel
$post->clearMediaChannel();
// Detach all media of the specific channel
$post->clearMediaChannel('channel-name');
$post = Post::first();
$media = Media::find(1); // model instance or just an media id: 1, or array of id: [1, 2, 3] or a collection of media models
// Sync media in the default channel (the $post will have only $media and others will be removed)
$post->syncMedia($media);
MediaCollection::find(1);
MediaCollection::findByName('My Collection');
// Retrieve the bound media as well
MediaCollection::with('media')->find(1);
MediaCollection::with('media')->findByName('My Collection');
$collection = MediaCollection::first();
// You can just pass a media model / id / name or an iterable collection of those
// e.g. 1 or [1, 2] or $media or [$mediaOne, $mediaTwo] or 'media-name' or ['media-name', 'another-media-name']
$collection->attachMedia($media);
$collection = MediaCollection::first();
// You can just pass a media model / id / name or an iterable collection of those
// e.g. 1 or [1, 2] or $media or [$mediaOne, $mediaTwo] or 'media-name' or ['media-name', 'another-media-name']
$collection->detachMedia($media);
// Detach all media by passing null / bool / empty-string / empty-array
$collection->detachMedia([]);
$collection = MediaCollection::first();
// You can just pass media model / id / name
$collection->syncMedia($media);
// You can even pass iterable list / collection
$collection->syncMedia(Media::all())
$collection->syncMedia([1, 2, 3, 4, 5]);
$collection->syncMedia([$mediaSix, $mediaSeven]);
$collection->syncMedia(['media-name', 'another-media-name']);
// Synchronize to having zero media by passing null / bool / empty-string / empty-array
$collection->syncMedia([]);
use Intervention\Image\Image;
use FarhanShares\MediaMan\Facades\Conversion;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Conversion::register('thumb', function (Image $image) {
// you have access to intervention/image library,
// perform your desired conversions here
return $image->fit(64, 64);
});
}
}
class Post extends Model
{
use HasMedia;
public function registerMediaChannels()
{
$this->addMediaChannel('gallery')
->performConversions('thumb');
}
}
// getFirstMediaUrl() accepts two optional arguments: channel name & conversion name
// you should provide channel name & conversion name to get the url
$post->getFirstMediaUrl('gallery', 'thumb');
// if you have multiple media associated & need to retrieve URLs you can do it with getUrl():
$media = $post->getMedia();
// getUrl() accepts only one optional argument: name of the conversion
// you should provide the conversion name to get the url
$mediaOneThumb = $media[0]->getUrl('thumb');