Netra
Netra

Reputation: 338

Laravel 5 Passing a variable from controller to view

I'm new to php platforms and what I'm trying to do is to pass a variable from the controller to view for populating a dropdown list with entries from the database, but whatever i'm trying is not working. I get the following error:

Undefined variable: products_create

I can't understand what I'm doing wrong.

Controller

public function create()
{
    $products_create = categories::all(['id', 'category']);

    return View::make('products.create', compact('id', 'category'));
}   

View

{!! Form::label('category', 'Categorie') !!}
{!! Form::select('category', $products_create) !!}

Upvotes: 2

Views: 1803

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 152860

You pass variables to a view in the form of an associative array:

return View::make('products.create', ['products_create' => $products_create]);

compact is a function that helps you build such an array where all keys are the same as the variable name. However you have to pass the actual variable name to the function:

return View::make('products.create', compact('products_create'));

Upvotes: 3

Related Questions