PHP code example of sandeep816 / smart-media-library
1. Go to this page and download the library: Download sandeep816/smart-media-library 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/ */
sandeep816 / smart-media-library example snippets
use FilamentMediaLibrary\MediaLibraryPlugin;
public function panel(Panel $panel): Panel
{
return $panel
->default()
->id('admin')
->path('admin')
->plugins([
MediaLibraryPlugin::make(),
]);
}
use FilamentMediaLibrary\Support\MediaAuthorization;
MediaAuthorization::authorizeUsing('delete', fn ($user, $media) => $user->is_super_admin);
namespace App\Policies;
use App\Models\User;
use FilamentMediaLibrary\Models\Media;
class MediaPolicy
{
public function viewAny(User $user): bool { return true; }
public function create(User $user): bool { return $user->can('create_media'); }
public function update(User $user, Media $media): bool { return $user->id === $media->uploaded_by; }
public function delete(User $user, Media $media): bool { return $user->is_admin; }
public function restore(User $user, Media $media): bool { return $user->is_admin; }
public function forceDelete(User $user, Media $media): bool { return $user->is_super_admin; }
public function view(User $user, Media $media): bool { return true; }
}
// Only allow image selection
MediaPicker::make('avatar')->image();
// Only allow PDF documents
MediaPicker::make('manual')->document();
// Custom MIME types
MediaPicker::make('assets')->acceptedMediaTypes(['image/svg+xml', 'application/pdf']);
// Disable inline uploads inside the picker modal
MediaPicker::make('archive_photo')->allowUpload(false);
namespace App\Models;
use FilamentMediaLibrary\Concerns\InteractsWithMediaAttachments;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use InteractsWithMediaAttachments;
}
$post = Post::find(1);
// Get single media record
$heroMedia = $post->singleMediaFor('hero');
// Get collection of media records ordered by sort order
$gallery = $post->mediaFor('gallery');
// Get array of UUIDs
$uuids = $post->mediaUuidsFor('gallery');