composer require spatie/laravel-medialibrary:^7.0.0

php artisan vendor:publish –provider=”Spatie\MediaLibrary\MediaLibraryServiceProvider” –tag=”migrations”

php artisan migrate

php artisan vendor:publish –provider=“Spatie\MediaLibrary\MediaLibraryServiceProvider” –tag=“migrations” php artisan migrate

Example Avtar.

add in register blade file.

 

do not forgot to add > enctype=”multipart/form-data” in form

<div class=”form-group row”>
<label for=”avatar” class=”col-md-4 col-form-label text-md-right”>{{ __(‘Avatar (optional)’) }}</label>

<div class=”col-md-6″>
<input id=”avatar” type=”file” class=”form-control” name=”avatar”>
</div>
</div>

 

Include >> in user model

use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\Models\Media;

Add in class user model

class User extends Authenticatable implements HasMedia
{
use Notifiable;
use HasMediaTrait;

//You can add different media of avtar //thumb,home etc

public function registerMediaConversions(Media $media = null)
{
$this->addMediaConversion(‘thumb’)
->width(50)
->height(50);

$this->addMediaConversion(‘home’)
->width(200)
->height(200);
}

}

Add avtar at registerController.

protected function create(array $data)
{
$user = User::create([
‘name’ => $data[‘name’],
’email’ => $data[’email’],
‘password’ => Hash::make($data[‘password’]),
]);

if (isset($data[‘avatar’])) {
$user->addMediaFromRequest(‘avatar’)->toMediaCollection(‘avatars’);
}
return $user;

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...