如何显示来自存储文件夹Laravel的图像

时间:2019-07-02 01:08:31

标签: php laravel laravel-5

我正在尝试从存储中检索图像到视图,但没有显示图像,在控制台中,我看到此错误WindowManager。我已经尝试了许多解决方案,包括this,但仍然无法使用。

控制器

# Set post meta
$ wp post meta set 123 _wp_page_template about.php
Success: Updated custom field '_wp_page_template'.

# Get post meta
$ wp post meta get 123 _wp_page_template
about.php

# Update post meta
$ wp post meta update 123 _wp_page_template contact.php
Success: Updated custom field '_wp_page_template'.

# Delete post meta
$ wp post meta delete 123 _wp_page_template
Success: Deleted custom field.

ProductsPhotos.php

Failed to load resource: the server responded with a status of 404 (Not Found)

Product.php

 class productController extends Controller
 {

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
 public function index(Request $request)
 {
    $userId = $request->user()->id;
    $products = product::where('admin', $userId)->get();
   return view('admin.product.index',compact('products'));
  }

  public function admin()
  {
   $products=product::all();
   return view('admin.product.index',compact('products'));
   }



  public function show($id)
 {
  $product = ProductsPhoto::findOrFail($id);
  return view('productsPhoto.show', compact('product'));

  }
/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
  public function store(Request $request) 
  { 

    $product = Product::create($request->all());
    foreach ($request->photos as $photo) {
        $filename = $photo->store('photos');
        ProductsPhoto::create([
            'product_id' => $product->id,
            'filename' => $filename
        ]);
      }

刀片

  class ProductsPhoto extends Model
  {
  protected $fillable = ['product_id', 'filename'];

   public function product()
   {
    return $this->belongsTo('App\Product');
    }

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

运行此artisan命令php artisan storage:link。在Blade中,使用下面的Storage Facade:

 @foreach($products as $product)
     <img src="{{ \Storage::url($products->productPhoto->filename) }}"
 @endforeach

/ *更新* /

 public function index(Request $request)
 {
    $products = product::where('admin', \Auth::id())->firstOrFail();
   return view('admin.product.index',compact('products'));
  }