You save the avatar before you assign any values to it.
Move this line
$avatar->save();
To just before your return statement.
I have used the media library package. when I tried to store an image it does not storing the image can anyone tell how to store images? this is my store function for medialibrary
public function store(Request $request)
{
$avatar = new Media();
$avatar->save();
if($request->hasFile('image') && $request->file('image')->isValid()){
$avatar->addMediaFromRequest('image')->toMediaCollection('media_lib');
}
// dd($avatar);
return redirect()->route('gallary.index');
}
This is my route
Route::resource('/gallary','MediaController')->middleware('auth');
This is my blade file
<form action="{{route('gallary.store')}}" method="post" enctype="multipart/form-data">
@csrf
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalCenterTitle">Add Media</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label for="image">Upload Image</label>
<input type="file" class="form-control-file" id="file" name="image">
<label for="imageSize" style="color: #9e9b9b">Maximum upload file size: 2 MB</label>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</div>
</form>
And this is my modal
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
class Media extends Model implements HasMedia
{
use HasMediaTrait;
}
Please or to participate in this conversation.