PHP code example of minemindmedia / laravel-mmmedia
1. Go to this page and download the library: Download minemindmedia/laravel-mmmedia 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/ */
minemindmedia / laravel-mmmedia example snippets
use Mmmedia\Media\Support\HasMediaAttachments;
class Product extends Model {
use HasMediaAttachments;
}
use Mmmedia\Media\Filament\Forms\Components\MediaPicker;
MediaPicker::make('featured_image_id')
->label('Featured Image')
->fieldKey('featured_image')
->multiple(false)
->allowUpload(true);
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class Product extends Model implements HasMedia
{
use InteractsWithMedia;
// MediaPicker will automatically work with your Spatie collections
}
use Mmmedia\Media\Support\HasMediaAttachments;
class Product extends Model
{
use HasMediaAttachments;
}
// In config/media.php
'allow_custom_model' => false,
// Or in your .env file
MEDIA_ALLOW_CUSTOM_MODEL=false
use Mmmedia\Media\Support\MediaItemCompatibility;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class MediaItem extends Model implements HasMedia
{
use InteractsWithMedia, MediaItemCompatibility;
// Your custom implementation
public function getThumbnailUrlAttribute(): ?string
{
// Your custom thumbnail logic
return $this->customThumbnailMethod();
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Mmmedia\Media\Support\HasMediaAttachments;
class Product extends Model
{
use HasMediaAttachments;
// Your model code...
}
namespace App\Filament\Resources;
use Filament\Forms;
use Filament\Forms\Form;
use Mmmedia\Media\Filament\Forms\Components\MediaPicker;
class ProductResource extends Resource
{
public static function form(Form $form): Form
{
return $form
->schema([
// Single image picker
MediaPicker::make('featured_image_id')
->label('Featured Image')
->fieldKey('featured_image')
->multiple(false)
->allowUpload(true),
// Multiple image gallery
MediaPicker::make('gallery_ids')
->label('Gallery')
->fieldKey('gallery')
->multiple(true)
->maxFiles(10)
->allowUpload(true)
->reorderable(true),
// Document picker
MediaPicker::make('document_id')
->label('Document')
->fieldKey('document')
->multiple(false)
->allowedMimes(['application/pdf', 'application/msword'])
->allowUpload(true),
]);
}
}
use App\Models\Product;
use Mmmedia\Media\Models\MediaItem;
$product = Product::find(1);
// Get media items
$featuredImage = $product->getFirstMedia('featured_image');
$gallery = $product->getMedia('gallery');
// Attach media
$mediaItem = MediaItem::find('some-id');
$product->attachMedia('featured_image', $mediaItem);
// Sync media (replace existing)
$product->syncMedia('gallery', ['media-id-1', 'media-id-2']);
// Check if model has media
if ($product->hasMedia('featured_image')) {
// Do something
}
// Get media count
$count = $product->getMediaCount('gallery');
// Detach media
$product->detachMedia('featured_image');
MediaPicker::make('field_name')
->multiple(true) // Allow multiple selection
->allowUpload(true) // Enable direct upload
->maxFiles(5) // Maximum number of files
->allowedMimes(['image/jpeg']) // Restrict file types
->fieldKey('custom_key') // Custom field key for usage tracking
->group('gallery') // Group media items
->reorderable(true); // Enable reordering
// Upload a file
POST /api/media/upload
Content-Type: multipart/form-data
// Update media metadata
PUT /api/media/{mediaItem}
{
"alt": "New alt text",
"title": "New title",
"caption": "New caption"
}
// Delete media item
DELETE /api/media/{mediaItem}
// Get all usages of a media item
$mediaItem = MediaItem::find('some-id');
$usages = $mediaItem->usages;
foreach ($usages as $usage) {
echo "Used in: {$usage->model_type} #{$usage->model_id}";
echo "Field: {$usage->field_key}";
echo "Position: {$usage->position}";
}
use Mmmedia\Media\Events\MediaItemCreated;
use Mmmedia\Media\Events\MediaItemUpdated;
use Mmmedia\Media\Events\MediaItemDeleted;
// Listen for events
Event::listen(MediaItemCreated::class, function ($mediaItem) {
// Generate thumbnails, etc.
});