Created
January 25, 2026 22:48
-
-
Save ziadoz/c38046aa4398022f048877620631b0e4 to your computer and use it in GitHub Desktop.
Laravel 12.x - Spatie Media User Avatars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Models; | |
| // use Illuminate\Contracts\Auth\MustVerifyEmail; | |
| use Illuminate\Database\Eloquent\Factories\HasFactory; | |
| use Illuminate\Foundation\Auth\User as Authenticatable; | |
| use Illuminate\Notifications\Notifiable; | |
| use Spatie\Image\Enums\Fit; | |
| use Spatie\MediaLibrary\HasMedia; | |
| use Spatie\MediaLibrary\InteractsWithMedia; | |
| use Spatie\MediaLibrary\MediaCollections\Models\Media; | |
| class User extends Authenticatable implements HasMedia | |
| { | |
| use InteractsWithMedia; | |
| /** @use HasFactory<\Database\Factories\UserFactory> */ | |
| use HasFactory, Notifiable; | |
| protected $fillable = [ | |
| 'name', | |
| 'email', | |
| 'password', | |
| ]; | |
| protected $hidden = [ | |
| 'password', | |
| 'remember_token', | |
| ]; | |
| protected function casts(): array | |
| { | |
| return [ | |
| 'email_verified_at' => 'datetime', | |
| 'password' => 'hashed', | |
| ]; | |
| } | |
| public function registerMediaCollections(): void | |
| { | |
| $this | |
| ->addMediaCollection('avatar') | |
| ->storeConversionsOnDisk('public') | |
| ->singleFile(); | |
| } | |
| public function registerMediaConversions(?Media $media = null): void | |
| { | |
| $this | |
| ->addMediaConversion('thumb') | |
| ->performOnCollections('avatar') | |
| ->fit(Fit::Contain, 150, 150) | |
| ->deferred(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use App\Models\User; | |
| use Illuminate\Support\Facades\Route; | |
| Route::get('/user', function() { | |
| $csrf = csrf_field(); | |
| return <<<FORM | |
| <form method="post" action="/user" enctype="multipart/form-data"> | |
| <input type="file" name="avatar"> | |
| {$csrf} | |
| <button>Save</button> | |
| </form> | |
| FORM; | |
| }); | |
| Route::post('/user', function() { | |
| $user = User::query()->where('email', '=', 'foo@bar.com')->first(); | |
| $user->addMediaFromRequest('avatar')->toMediaCollection('avatar'); | |
| return redirect()->route('user.show', $user); | |
| }); | |
| Route::get('/user/{user}', function () { | |
| $user = User::query()->where('email', '=', 'foo@bar.com')->first(); | |
| return <<<HTML | |
| <p>{$user->name}</p> | |
| <p><img src="{$user->getFirstMedia('avatar')->getUrl('thumb')}"></p> | |
| HTML; | |
| })->name('user.show'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment