1. Go to this page and download the library: Download carone/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/ */
'management_middleware' => ['auth'], // Require authentication
// or
'management_middleware' => ['auth', 'admin'], // Require admin role
use Carone\Media\Facades\Media;
// Store a new media file with tags
$media = Media::store([
'file' => $uploadedFile,
'name' => 'My Image',
'description' => 'A beautiful landscape photo',
'type' => 'image',
'tags' => ['Nature', 'Photography', 'Landscape'], // Optional tags
]);
// Get media by ID
$media = Media::getById(1);
// Get media by type with pagination
$result = Media::getByType('image', limit: 10, offset: 0);
// Search media
$result = Media::search('landscape', type: 'image', limit: 5);
// Serve media file
return Media::serve('image', 'filename.jpg');
// Serve thumbnail
return Media::thumbnail('image', 'filename.jpg');
// Delete media
Media::delete(1);
// Delete multiple media files
$result = Media::deleteMultiple([1, 2, 3]);
// Delete all media of a specific type
$result = Media::deleteByType('image');
// Clean up orphaned files
$result = Media::cleanupOrphanedFiles('image');
// Get enabled media types
$types = Media::getEnabledTypes();
use Carone\Media\Facades\Media;
// Upload image with tags
$media = Media::store([
'type' => 'image',
'file' => $request->file('image'),
'name' => 'Sunset Photo',
'tags' => ['Nature', 'Photography', 'Sunset'], // Tags are automatically created if they don't exist
]);
// Upload external media with tags
$media = Media::store([
'type' => 'video',
'source' => 'external',
'url' => 'https://youtube.com/watch?v=example',
'name' => 'Tutorial Video',
'tags' => ['Tutorial', 'Education'],
]);
// Get all tags for a media resource
$tags = $media->tags; // Collection of Tag models
// Get tag names
$tagNames = $media->tags->pluck('name')->toArray();
// ['Nature', 'Photography', 'Sunset']
// Get tag slugs
$tagSlugs = $media->tags->pluck('slug')->toArray();
// ['nature', 'photography', 'sunset']
// Sync tags (replaces existing tags)
$media->syncTags(['New Tag', 'Another Tag']);
// The old tags are removed, only the new ones remain
'tags' => [
'enabled' => true, // Set to false to disable tagging functionality
],
try {
$media = Media::store($data);
} catch (\InvalidArgumentException $e) {
// Invalid media type or validation failed
} catch (\Exception $e) {
// Other storage errors
}
use Carone\Media\Actions\StoreMediaAction;
// Upload a local image with tags
$media = StoreMediaAction::run([
'type' => 'image',
'file' => $request->file('image'),
'name' => 'My Beautiful Image',
'description' => 'A description of the image',
'tags' => ['Nature', 'Landscape', 'Photography'],
]);
// Upload external media with tags
$media = StoreMediaAction::run([
'type' => 'video',
'source' => 'external',
'url' => 'https://www.youtube.com/watch?v=example',
'name' => 'External Video',
'tags' => ['Tutorial', 'Video'],
]);
use Carone\Media\Actions\GetMediaAction;
// Get media by type with pagination
$images = GetMediaAction::byType('image', $limit = 20, $offset = 0);
// Get single media item (
use Carone\Media\Actions\DeleteMediaAction;
// Delete single media (also removes tag associations)
$success = DeleteMediaAction::run($mediaId);
// Bulk delete
$result = DeleteMediaAction::make()->deleteMultiple([1, 2, 3]);
// Before (your existing controller)
public function uploadMedia(MediaUploadRequest $request)
{
// ... complex upload logic ...
}
// After (using this package)
public function uploadMedia(Request $request)
{
$media = StoreMediaAction::run([
'type' => $request->input('type'),
'file' => $request->file('file'),
'name' => $request->input('name'),
'tags' => $request->input('tags', []), // Optional tags
]);
return response()->json([
'success' => true,
'media' => $media,
'tags' => $media->tags->pluck('name'),
]);
}