无法将值从Controller传递给.blade.php

时间:2015-12-11 07:46:58

标签: php laravel-5

我正在尝试显示所有产品名称,
我收到了错误:

ErrorException in 4938c589ea3db6bab0c4c788473314a4 line 46:
Undefined variable: allproduct (View: C:\Server\WebDocs\CRUD\resources\views\product.blade.php)

Error MSG

我试过return View::make('product',$allproduct);return View::make('product')->with('product',allproduct);也不能,我不知道为什么,这是我的第一个Laravel计划。

在我的 ProductController

    public function index()
{
    // get all the Products
    $allproduct = Product::all();

    return View::make('product',$allproduct);
}

在我的 product.blade.php

 <table class="table table-striped table-bordered">
          <thead>
              <tr>
                  <td>No</td>
                  <td>Product</td>
                  <td>Actions</td>
              </tr>
          </thead>
          <tbody>
          @foreach($allproduct as $key => $value)
              <tr>
                  <td>$key</td>
                  <td>{{ $value->product }}</td>
                  <!-- we will also add show, edit, and delete buttons -->
                  <td>
                  </td>
              </tr>
          @endforeach
          </tbody>
      </table>

4 个答案:

答案 0 :(得分:2)

你可以试试这个,

return View::make('product', compact('allproduct'));

答案 1 :(得分:1)

尝试在Controller中使用此代码:

return View::make('product')->with(['allproduct' => $allproduct]);

答案 2 :(得分:0)

尝试传递像

这样的变量
   public function index()
{
    // get all the Products
    $allproduct = Product::all();

    return View::make('product')->with('allproduct', $allproduct);
}

并在视野中

仅仅执行@foreach($allproduct as $product)然后在<td><td>{{$product->product}}</td>。并确保您在product

中有db

答案 3 :(得分:0)

控制器:

callbackURL := "https://" + request.Host + request.URL.String()

刀片:

 return View::make('product')->with('allproduct',$allproduct);
相关问题