PHP code example of dialloibrahima / laravel-model-media
1. Go to this page and download the library: Download dialloibrahima/laravel-model-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/ */
dialloibrahima / laravel-model-media example snippets
namespace App\Models;
use DialloIbrahima\HasMedia\HasMedia;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasMedia;
protected static function booted()
{
self::registerMediaForColumn(
column: 'profile_photo', // Your DB column
directory: 'avatars', // Storage folder
fileName: 'id', // Name file after a model attribute (or use a Closure)
disk: 'public' // Optional: storage disk (default is 'public')
);
}
}
public function update(Request $request, User $user)
{
if ($request->hasFile('photo')) {
// This method automatically saves the model for you!
$user->attachMedia($request->file('photo'), 'profile_photo');
}
return back();
}
// Remove media and clear the column
$user->detachMedia('profile_photo');
// Media is also automatically deleted when the model is deleted
$user->delete(); // Files are cleaned up automatically
protected static function registerMediaForColumn(
string $column, // Database column name
string $directory, // Storage directory
string|Closure $fileName, // Attribute name or Closure for filename
string $disk = 'public' // Storage disk
): void
public function attachMedia(UploadedFile $file, string $column): bool
public function detachMedia(?string $column): bool
public function getMediaUrl(string $column): ?string
public function getMediaMappings(): array
public function getGlideUrl(
string $column, // Column name
array $params = [], // Glide transformation parameters
bool $throwOnError = false // Throw exception on error
): ?string
public function getGlidePresetUrl(
string $column, // Column name
string $preset, // Preset name from config
bool $throwOnError = false // Throw exception on error
): ?string
// Use thumbnail preset (defined in config)
$user->getGlidePresetUrl('avatar', 'thumbnail');
// Use medium preset
$user->getGlidePresetUrl('cover', 'medium');
public function getGlideSrcset(
string $column, // Column name
array $widths = [400, 800, 1200, 1600], // Image widths
bool $throwOnError = false // Throw exception on error
): ?string
// Store avatars on public disk
self::registerMediaForColumn(
column: 'avatar',
directory: 'avatars',
fileName: 'id',
disk: 'public'
);
// Store private documents on S3
self::registerMediaForColumn(
column: 'contract',
directory: 'contracts',
fileName: fn ($model) => "contract-{$model->id}",
disk: 's3'
);
use DialloIbrahima\HasMedia\HasMedia;
use DialloIbrahima\HasMedia\Plugins\Glide\HasGlideUrls;
class User extends Model
{
use HasMedia, HasGlideUrls;
// ...
}
// Resize to 200x200
$url = $user->getGlideUrl('avatar', ['w' => 200, 'h' => 200]);
// Crop to fit
$url = $user->getGlideUrl('avatar', ['w' => 300, 'h' => 300, 'fit' => 'crop']);
// Use a preset defined in config
$url = $user->getGlidePresetUrl('avatar', 'thumbnail');
// Get a responsive srcset
$srcset = $user->getGlideSrcset('avatar');
use DialloIbrahima\HasMedia\Plugins\Glide\Facades\Glide;
// Generate a URL for a specific path
$url = Glide::url('avatars/user-1.jpg', ['w' => 200, 'fit' => 'crop']);
// Use a preset
$url = Glide::preset('posts/cover.png', 'medium');
// Generate a responsive srcset
$srcset = Glide::srcset('products/hero.webp', [400, 800, 1200]);
// Clear cache for a specific file
Glide::deleteCache('avatars/user-1.jpg');