【问题标题】:Passing arguments from route through view via controller doesn't work通过控制器通过视图从路由传递参数不起作用
【发布时间】:2014-05-12 21:44:55
【问题描述】:

我正在创建一个 Laravel 4 webapp 并获得以下路线:

Route::get('products/{whateverId}', 'ProductController@index');

这是我在 ProductController 中的索引函数:

    public function index($whateverId)
{
    $products = Product::all();
    $data['whateverId'] = $whateverId;
    return View::make('products', compact('products'), $data);
}

在我看来,这会返回以下错误:

<p>Product: {{ $data['product'] }}</p>

错误异常 未定义变量:数据(查看:/Users/myuser/webapp/app/views/products.blade.php)

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案1】:
    return View::make('products', compact('products'), "data"=>$data);
    

    (或compact('data')

    【讨论】:

      【解决方案2】:

      尝试将其传递为:

      $data['whateverId'] = $whateverId;
      
      $data['products'] = Product::all();;
      
      return View::make('products', $data);
      

      你可以访问它

      {{ foreach($products as ...) }}
      

      {{ $whateverId }}
      

      或者你可以

      $products = Product::all();
      
      $data['whateverId'] = $whateverId;
      
      return View::make('products')
               ->with('products', $products)
               ->with('whateverId', $whateverId);  
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-17
        • 1970-01-01
        • 1970-01-01
        • 2015-04-28
        • 2020-04-03
        • 1970-01-01
        • 2018-07-25
        • 2014-10-05
        相关资源
        最近更新 更多