i am trying to upload an image to my database the problem is when i use the validator , it always return tru showing the error the avatar file should be an image even when i am puttin an image , i tried jpeg,pjg and png but nothing seems to work
controller
public function store(Request $request)
{
$this -> validate($request ,[
//other validators working fine
'avatar' => ['nullable', 'image' ],
]);
if($request->hasFile('avatar')){
// Get filename with the extension
$filenameWithExt = $request->file('avatar')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('avatar')->getClientOriginalExtension();
// Filename to store
$fileNameToStore= $filename.'_'.time().'.'.$extension;
// Upload Image
$path = $request->file('avatar')->storeAs('public/avatars', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
$member= new Member;
//other inputs working fine
$member->avatar = $fileNameToStore;
$member->save();
}
view
<div class="form-group row">
<label for="avatar" class="col-md-4 col-form-label text-md-right">image du membre</label>
<div class="col-md-6">
<input id="avatar" type="file" class=" @error('avatar') is-invalid @enderror" name="avatar" autofocus>
@error('avatar')
<span class="invalid-feedback" role="alert">
<strong></strong>
</span>
@enderror
</div>
</div>
when i tired removing the validators it works and always getting noimage in the database
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire