简体   繁体   中英

how to fetch image from storage folder to blade in laravel 8?

I am facing an error while showing back image stored in storage folder in laravel. so here is my code of storing image in storage folder :

    public function store(Request $request)
{

    $data = $request->all();
    // dd($request->Image);

    $User =  Auth::user()->id;
    
    $rules = array(
       'postname' => 'required' ,
       'description' =>'required',
       'Image' =>  'required|image|mimes:jpeg,png,jpg|max:8192',
    );

    $validate=Validator::make($data,$rules);
    
    if ($validate->fails()) {
        return redirect()->back()->withInput()->withErrors($validate);
    }
    else{
       
       $path =   Storage::disk('local')->put('public',$request->file('Image'));
      
       $form_data = array(
           'title' => $data['postname'],
           'description' => $data['description'],
           'Image'  => $path,
           'user_id' => $User,
       );
    //    dd($form_data);
       $Posts = Post::create($form_data);
       $Message = "successfully added";
       return redirect('/Admin/Posts')->with('success',$Message);
    }   
}

and here is the blade file for retreiving image :

              <img class="card-img-bottom" src="{{ Storage::url($Posts->Image)}}" width="200" height="450">

here is the link in config/filesystems.php

'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

and here is the view of my table where data has been stored : enter image description here

and its been showing in storage/app/images folder , but when I am trying to get it back from public/storage , their is no photo. I have made the storage link of that?? now how to fetch it from the database??

here is the view method of it:

      public function index(Request $request)
{
    $Post = Post::get();
    // dd($Post);
    $User = User::get();
    return view('Posts.index',compact('Post'));
}

and here is the inspect of image in browser:

   <img src="/storage/1dTAYokONfbLXS74vEcus665uBhPBzVDAe53QMkL.jpg" width="200" height="450" class="card-img-bottom">

and here it the ui of image not being rendered on blade: enter image description here

and from laravel documentation :I have done

      php artisan storage:link

here is the storage folder of it: enter image description here

and here is the public folder with subfolder called storage folder,where the files can be seen by user for fetching in blade.. : enter image description here

any help ? how to solve it??

error solved, what I did is, followed this stackoverflow answer: enter link description here and my link got worked again. I just removed the storage folder from the public folder and and again run the command

php artisan storage:link

and it solved my error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM