Question

I'm creating a Laravel 4 webapp and got the following route:

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

This is my index-function in ProductController:

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

In my view, this returns the following error:

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

ErrorException Undefined variable: data (View: /Users/myuser/webapp/app/views/products.blade.php)

Était-ce utile?

La solution

return View::make('products', compact('products'), "data"=>$data);

(or compact('data'))

Autres conseils

Try passing it as:

$data['whateverId'] = $whateverId;

$data['products'] = Product::all();;

return View::make('products', $data);

And you'll have acces to it as

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

and

{{ $whateverId }}

Or you can

$products = Product::all();

$data['whateverId'] = $whateverId;

return View::make('products')
         ->with('products', $products)
         ->with('whateverId', $whateverId);  
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top