Image does not showing up at blade

  laravel, laravel-8, php

I wanted to udpate image row of my users table. Here is my form:

<form method="POST" action="{{ route('profile.update' , ['profile' => Auth::user()->id]) }}" enctype="multipart/form-data">
   @csrf
   @method('PATCH')
   <input type="file" class="form-control-file" id="exampleFormControlFile1" name="image">
   <button type="submit" class="btn btn-primary">
        {{ __('Submit') }}
    </button>
</form>

Then I have this update method in my Controller:

public function update(Request $request, $profile)
{
    $validate_data = Validator::make($request->all(),[
        'image' => 'image|nullable|max:1999',
        'location' => 'nullable'
    ]);

    if($request->hasFile('image')){
        // get filename with extension
        $filenameWithExt = $request->file('image')->getClientOriginalName();
        // get just filename
        $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
        // get just ext
        $extension = $request->file('image')->getClientOriginalExtension();
        // filename to store
        $fileNameToStore = $filename.'_'.time().'.'.$extension;
        // upload
        $path = $request->file('image')->storeAs('public/avatars', $fileNameToStore);
    }else{
        $fileNameToStore = 'noimage.jpg';
    }

    $user = User::findOrFail($profile);

    $user->update([
        'location' => request('location'),
        'image' => $fileNameToStore
    ]);

    $user->save();

    return back();
}

I have also ran the command php artisan storage:link to make the storage folder at public directory.

So whenever I try to upload a picture, the image is stored successfully at public/storage and at the database, and in order to retrieve it at blade, I did this:

<img
src="/storage/avatars/{{ Auth::user()->img }}"
alt="{{ Auth::user()->img }}"
/>

But the problem is, it does not show up somehow!!

So what’s going wrong here, how can I display my image from db properly?

I would really appreciate any idea or suggestion from you guys…

Thanks in advance.

Source: Laravel

Leave a Reply