1. Go to this page and download the library: Download waad/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/ */
waad / media example snippets
use Waad\Media\HasMedia;
class Post extends Model
{
use HasMedia;
}
// Upload a single file
$media = $post->addMedia($request->file('image'))->upload();
// Upload multiple files
$mediaItems = $post->addMedia($request->file('images'))->upload();
// Upload to a specific collection
$media = $post->addMedia($request->file('image'))
->collection('avatars')
->upload();
// Upload with custom options
$media = $post->addMedia($request->file('image'))
->collection('gallery')
->disk('s3')
->bucket('photos')
->label('Profile Picture')
->index(3)
->upload();
use Waad\Media\HasMedia;
class User extends Model
{
use HasMedia;
public function registerCollections(array $attributes = []): array
{
return [
'avatar' => [
'disk' => 's3',
'bucket' => 'avatars', // directory in the bucket if whant save direct on bucket set empty string ''
'label' => 'User Avatar',
'single' => true, // Only keeps one file
's3' => [
'ttl_temporary_url' => config('media.s3.default_ttl_temporary_url', 5),
],
],
'gallery' => [
'disk' => 'public',
'bucket' => 'photos',
'label' => 'Photo Gallery',
'single' => false, // Allows multiple files
],
];
}
}
// Get all media
$allMedia = $post->getMedia();
// Get media from a specific collection
$avatars = $post->getCollection('avatars');
// Get collection urls
$urls = $post->getCollectionUrls('avatars');
$urls = $post->getCollectionGroupUrls(); // get urls for multiple collections
$urls = $post->getCollectionGroupUrls(only: ['avatar', 'gallery']); // get urls for multiple collections
$urls = $post->getCollectionGroupUrls(except: ['avatar', 'gallery']); // get urls for multiple collections
// Get all collections by group
$collections = $post->getCollectionGroups();
$collections = $post->getCollectionGroups(only: ['avatar', 'gallery']); // only return the collections in the array
$collections = $post->getCollectionGroups(except: ['avatar', 'gallery']); // return all collections except the ones in the array
// Get collection as array
$array = $post->getCollectionArray('avatars');
// Get first or last media
$first = $post->getFirstMedia();
$last = $post->getLastMedia();
// Get first or last media by collection
$first = $post->getFirstMediaByCollection('gallery');
$last = $post->getLastMediaByCollection('gallery');
// Check if model has media in a collection
$post->hasMedia('avatars'); // true / false
// Find by ID (supports withTrashed)
$media = $post->mediaById($id);
$media = $post->mediaById($id, withTrashed: true);
// Filter by MIME type
$images = $post->mediaByMimeType('image/jpeg');
// Filter by MIME type and collection
$images = $post->mediaByMimeTypeByCollection('image/jpeg', 'gallery');
$images = $post->mediaByMimeTypeByCollection('image/jpeg', 'gallery', withTrashed: true);
// Filter by approval status
$approved = $post->mediaApproved();
$disapproved = $post->mediaApproved(false);
// Statistics
$totalSize = $post->mediaTotalSize();
$totalCount = $post->mediaTotalCount();
$totalCount = $post->mediaTotalCount(withTrashed: true);
// Statistics by collection
$size = $post->mediaTotalSizeByCollection('gallery');
$size = $post->mediaTotalSizeByCollection('gallery', withTrashed: true);
$count = $post->mediaTotalCountByCollection('gallery');
$count = $post->mediaTotalCountByCollection('gallery', withTrashed: true);
// Order by index (enabled by default)
$media = $post->orderByIndexMedia()->getMedia();
$media = $post->orderByIndexMedia()->getCollection('gallery');
$urls = $post->orderByIndexMedia()->getCollectionUrls();
$array = $post->orderByIndexMedia()->getCollectionArray('gallery');
// Works with filter methods too
$images = $post->orderByIndexMedia()->mediaByMimeType('image/jpeg');
$approved = $post->orderByIndexMedia()->mediaApproved();
// Disable index ordering
$media = $post->orderByIndexMedia(false)->getMedia();
$post->syncMedia($request->file('images'), [$oldMediaId1, $oldMediaId2])->upload();
$post->syncMedia($request->file('image'), [$oldMediaId])->collection('avatars')->upload();
// Illuminate\Support\Collection of IDs is also accepted
$post->syncMedia($file, collect([$id1, $id2]))->upload();
// Delete specific media by ID (soft delete)
$post->deleteMedia($mediaId)->delete();
// Delete specific media by model
$post->deleteMedia($mediaModel)->delete();
// Delete multiple media by IDs
$post->deleteMedia([$id1, $id2, $id3])->delete();
// Delete all media for the model
$post->deleteMedia()->delete();
$media->approve(); // Mark as approved
$media->disApprove(); // Mark as disapproved
// Query approved media globally
$approved = \Waad\Media\Media::approved()->get();
$service = $post->addMedia(null);
// Check if a file exists
$service->fileExists('upload/photo.jpg');
// Get file size in bytes
$service->fileSize('upload/photo.jpg');
// Get file metadata (size, mimetype, last_modified)
$service->fileMetadata('upload/photo.jpg');
// Delete a file from disk
$service->deleteFile('upload/photo.jpg');
// Generate a temporary URL (S3 and compatible disks)
$service->disk('s3')->temporaryUrl('photos/secret.jpg', minutes: 10);
return [
// Media model class
'model' => \Waad\Media\Media::class,
// Database table name
'table_name' => 'media',
// Default storage settings
'disk' => env('MEDIA_DISK', 'public'),
'bucket' => env('MEDIA_BUCKET', 'upload'),
'default_collection' => env('MEDIA_DEFAULT_COLLECTION', 'default'),
// S3 configuration
's3' => [
'default_ttl_temporary_url' => env('MEDIA_DEFAULT_S3_TTL_TEMPORARY_URL', 5),
],
// Map disk names to public URL prefixes (used by media:link and full_url)
'shortcut' => [
// 'public' => 'storage',
],
// Append full_url attribute to the Media model
'enable_full_url' => env('MEDIA_ENABLE_FULL_URL', true),
// Auto-prune soft-deleted media after N days (via media:prune)
'prune_media_after_day' => env('MEDIA_PRUNE_MEDIA_AFTER_DAY', 30),
// Default approval status for newly uploaded media
'default_approved' => env('MEDIA_DEFAULT_APPROVED', true),
// Date format for created_at / updated_at serialization (null = Y-m-d H:i:s)
'format_date' => env('MEDIA_DATE_FORMAT', null),
];