minemindmedia/laravel-mmmedia
Composer 安装命令:
composer require minemindmedia/laravel-mmmedia
包简介
A Laravel package providing a global media library and Filament MediaPicker field
README 文档
README
A comprehensive Laravel package that provides a global media library and Filament MediaPicker field for managing media assets in your Laravel applications.
Features
- 🖼️ Global Media Library - Centralized media management with Filament admin interface
- 📁 MediaPicker Field - Easy-to-use Filament form component for media selection
- 🔗 Usage Tracking - Track where media items are used across your application
- 📤 File Upload - Direct upload support with drag & drop
- 🎨 Multiple File Types - Support for images, videos, and documents
- 🔄 Reordering - Reorder media items in galleries
- 🏷️ Metadata - Alt text, titles, captions, and custom metadata
- 💾 Flexible Storage - Works with any Laravel filesystem (local, S3, etc.)
- 🎯 Model Integration - Easy integration with any Eloquent model
- 🚀 Filament v4 Compatible - Native support for Filament v4
- 📚 Spatie MediaLibrary Integration - Works with or without Spatie MediaLibrary
- 🖼️ Thumbnail Generation - Automatic thumbnail generation with Intervention Image
Installation
Option 1: Install from Packagist (Recommended)
composer require minemindmedia/laravel-mmmedia
Option 2: Install directly from GitHub
Add the repository to your composer.json:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/minemindmedia/laravel-mmmedia.git"
}
]
}
Then install:
composer require minemindmedia/laravel-mmmedia:dev-main
Publish the configuration file:
php artisan vendor:publish --provider="Mmmedia\Media\MediaServiceProvider" --tag=media-config
Publish and run the migrations:
php artisan vendor:publish --provider="Mmmedia\Media\MediaServiceProvider" --tag=media-migrations
php artisan migrate
Quick Start
- Add the trait to your model:
use Mmmedia\Media\Support\HasMediaAttachments; class Product extends Model { use HasMediaAttachments; }
- Use MediaPicker in your Filament form:
use Mmmedia\Media\Filament\Forms\Components\MediaPicker; MediaPicker::make('featured_image_id') ->label('Featured Image') ->fieldKey('featured_image') ->multiple(false) ->allowUpload(true);
- Access the Media Library: Visit your Filament admin panel and you'll see a new "Media" section where you can manage all media items.
Filament v4 Compatibility
This package is 100% natively compatible with Filament v4! No vendor patches required.
🎉 Complete native Filament v4 support achieved in v1.1.3!
Note: If you're using a custom MediaItem model, make sure the
generateThumbnail()method is public to avoid visibility conflicts.
Using with Spatie MediaLibrary
If you're already using Spatie MediaLibrary, the MediaPicker will automatically detect and work with your existing media:
use Spatie\MediaLibrary\HasMedia; use Spatie\MediaLibrary\InteractsWithMedia; class Product extends Model implements HasMedia { use InteractsWithMedia; // MediaPicker will automatically work with your Spatie collections }
Using with Package's Media System
If you prefer to use the package's built-in media system:
use Mmmedia\Media\Support\HasMediaAttachments; class Product extends Model { use HasMediaAttachments; }
Custom MediaItem Model
If you need to customize the MediaItem model, you can create your own and disable automatic binding:
// In config/media.php 'allow_custom_model' => false, // Or in your .env file MEDIA_ALLOW_CUSTOM_MODEL=false
Then create your custom model:
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(); } }
Configuration
The package configuration is located in config/media.php. Here are the key settings:
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', ], ];
Usage
1. Add the Trait to Your Models
Add the HasMediaAttachments trait to any model that needs media functionality:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Mmmedia\Media\Support\HasMediaAttachments; class Product extends Model { use HasMediaAttachments; // Your model code... }
2. Use the MediaPicker in Filament Forms
<?php 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), ]); } }
3. Working with Media in Your Code
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');
4. MediaPicker Configuration Options
The MediaPicker component supports various configuration options:
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
Media Library Admin
The package automatically registers a MediaItemResource in your Filament admin panel. You can:
- Browse all media items in a grid or list view
- Upload new media files
- Edit metadata (alt text, title, caption)
- View usage information
- Delete unused media items
- Filter by file type, usage status, etc.
API Endpoints
The package provides API endpoints for programmatic access:
// 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}
Advanced Usage
Custom Storage Paths
You can customize where different file types are stored:
// In config/media.php 'paths' => [ 'images' => 'uploads/images', 'videos' => 'uploads/videos', 'documents' => 'uploads/documents', ],
Thumbnail Generation
The package supports thumbnail generation for images:
// In config/media.php 'thumbnails' => [ 'enabled' => true, 'sizes' => [ 'small' => [150, 150], 'medium' => [300, 300], 'large' => [600, 600], ], 'quality' => 80, ],
Usage Tracking
The package automatically tracks where media items are used:
// 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}"; }
Events
The package fires events when media items are created, updated, or deleted:
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. });
Thumbnail Generation
The package supports automatic thumbnail generation using Intervention Image:
Install Intervention Image (Optional)
composer require intervention/image
Generate Thumbnails
# Generate thumbnails for all media items php artisan media:generate-thumbnails # Force regeneration of existing thumbnails php artisan media:generate-thumbnails --force
Custom Thumbnail Configuration
You can configure thumbnail generation in config/media.php:
'thumbnails' => [ 'enabled' => true, 'sizes' => [ 'small' => [150, 150], 'medium' => [300, 300], 'large' => [600, 600], ], 'quality' => 80, ],
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email security@minemindmedia.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Support
For support, please email support@minemindmedia.com or create an issue on GitHub.
Auto-update test
minemindmedia/laravel-mmmedia 适用场景与选型建议
minemindmedia/laravel-mmmedia 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 09 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「media」 「library」 「upload」 「images」 「videos」 「documents」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 minemindmedia/laravel-mmmedia 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 minemindmedia/laravel-mmmedia 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 minemindmedia/laravel-mmmedia 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
Simple Sharing generates social media share links within CP entry pages, allowing you to quickly & easily share entries.
Inbox pattern process implementation for your Laravel Applications
Laravel Media Popup to upload/view the files
Core library that defines common interfaces used by the rest of the intahwebz..
Asset Manager Laravel package for Gigcodes UI Library
统计信息
- 总下载量: 13
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-29