How to get file URL using Storage facade in laravel 5?

前端 未结 13 770
庸人自扰
庸人自扰 2020-11-30 22:40

I\'ve been experimenting using the new Flysystem integration with Laravel 5. I am storing \'localised\' paths to the DB, and getting the Storage facade to complete the path

13条回答
  •  自闭症患者
    2020-11-30 23:19

    Store method:

    public function upload($img){
       $filename = Carbon::now() . '-' .  $img->getClientOriginalName();
       return Storage::put($filename, File::get($img)) ? $filename : '';
    }
    

    Route:

     Route::get('image/{filename}', [
            'as'   => 'product.image',
            'uses' => 'ProductController@getImage',
        ]);
    

    Controller:

     public function getImage($filename)
        {
            $file = Storage::get($filename);
    
            return new Response($file, 200);
        }
    

    View:

     your image
    

提交回复
热议问题