Search code examples
phplaravelimage

Image in Laravel bug


my problem : i try to show the image in Laravel using the following code


        
   @foreach($data as $dat)
    <p>{{ $dat->name }}</p>
    <hr>
    <img src="{{ Storage::url($dat->avatar) }}" alt="">
    @endforeach 
  • i have linked the storage with the public

  • this how i save the image to data base with other data

public function createUser(CreateUserRequest $request){
        
        // dd($request);
    
        if($request->hasFile('avatar')){
            $image = $request->avatar;
            $path = Storage::disk('public')->put('avatar/',$request->file('avatar'));
            $userData = $request->all();
            $userData['avatar'] = $path;
            
            User::create($userData);
            return redirect()->back()->with('success_message','Success');
        }

        User::create($request->all());
            return redirect()->back()->with('success_message','Success');
        }
  • this is the string saved into data base
avatar/CU0TjohsMMm9VmmI1T3s1T1vUJkGjnXFlbvWejPT.jpg

the problem that i try the same code on my laptop the image showed correctly while on the pc i don't know what is wrong


Solution

  • Run the storage link command:
    
    php artisan storage:link
    
    This ensures storage/app/public is linked to public/storage.
    
    Check file permissions:
    Ensure Laravel can read the stored images:
    
    chmod -R 775 storage/app/public
    chmod -R 775 public/storage
    
    Use the correct URL function:
    Instead of Storage::url(), try:
    
    <img src="{{ asset('storage/' . $dat->avatar) }}" alt="">
    
    Check server differences:
    
    If using XAMPP, confirm that .htaccess is allowing access.
    If using Linux, ensure the web server user (www-data for Apache) has read access to storage/app/public.