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();
    }
}

return [
    // Default disk for storing media files
    'disk' => env('MEDIA_DISK', 'public'),
    
    // Upload settings
    'upload' => [
        'max_file_size' => env('MEDIA_MAX_FILE_SIZE', 10240), // KB
        'max_files' => env('MEDIA_MAX_FILES', 10),
        'allowed_mimes' => [
            'image' => ['image/jpeg', 'image/png', 'image/gif', 'image/webp'],
            'video' => ['video/mp4', 'video/avi', 'video/mov'],
            'document' => ['application/pdf', 'application/msword'],
        ],
    ],
    
    // Storage paths
    'paths' => [
        'images' => 'media/images',
        'videos' => 'media/videos',
        'documents' => 'media/documents',
    ],
];



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}

// In config/media.php
'paths' => [
    'images' => 'uploads/images',
    'videos' => 'uploads/videos',
    'documents' => 'uploads/documents',
],

// In config/media.php
'thumbnails' => [
    'enabled' => true,
    'sizes' => [
        'small' => [150, 150],
        'medium' => [300, 300],
        'large' => [600, 600],
    ],
    'quality' => 80,
],

// 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.
});

'thumbnails' => [
    'enabled' => true,
    'sizes' => [
        'small' => [150, 150],
        'medium' => [300, 300],
        'large' => [600, 600],
    ],
    'quality' => 80,
],
bash
php artisan vendor:publish --provider="Mmmedia\Media\MediaServiceProvider" --tag=media-config
bash
php artisan vendor:publish --provider="Mmmedia\Media\MediaServiceProvider" --tag=media-migrations
php artisan migrate