Hedge
Hedge

Reputation: 16748

Passing arguments from route through view via controller doesn't work

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)

Upvotes: 1

Views: 54

Answers (2)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87739

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);  

Upvotes: 1

Cranio
Cranio

Reputation: 9847

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

(or compact('data'))

Upvotes: 3

Related Questions