I have this site and one of its pages creates a simple list of people from the database. I need to add one specific person to a variable I can access.
How do I modif
This is how you do it:
function view($view)
{
$ms = Person::where('name', '=', 'Foo Bar')->first();
$persons = Person::order_by('list_order', 'ASC')->get();
return $view->with('persons', $persons)->with('ms', $ms);
}
You can also use compact():
function view($view)
{
$ms = Person::where('name', '=', 'Foo Bar')->first();
$persons = Person::order_by('list_order', 'ASC')->get();
return $view->with(compact('persons', 'ms'));
}
Or do it in one line:
function view($view)
{
return $view
->with('ms', Person::where('name', '=', 'Foo Bar')->first())
->with('persons', Person::order_by('list_order', 'ASC')->get());
}
Or even send it as an array:
function view($view)
{
$ms = Person::where('name', '=', 'Foo Bar')->first();
$persons = Person::order_by('list_order', 'ASC')->get();
return $view->with('data', ['ms' => $ms, 'persons' => $persons]));
}
But, in this case, you would have to access them this way:
{{ $data['ms'] }}
Came across a similar problem, but if you do not necessarily want to return a view with view file, you can do this:
return $view->with(compact('myVar1', 'myVar2', ..... , 'myLastVar'));
Use compact
function view($view)
{
$ms = Person::where('name', '=', 'Foo Bar')->first();
$persons = Person::order_by('list_order', 'ASC')->get();
return View::make('users', compact('ms','persons'));
}
For passing multiple array data from controller to view, try it. It is working. In this example, I am passing subject details from a table and subject details contain category id, the details like name if category id is fetched from another table category.
$category = Category::all();
$category = Category::pluck('name', 'id');
$item = Subject::find($id);
return View::make('subject.edit')->with(array('item'=>$item, 'category'=>$category));
with
function and single
parameters:
$ms = Person::where('name', 'Foo Bar');
$persons = Person::order_by('list_order', 'ASC')->get();
return $view->with(compact('ms', 'persons'));
with
function and array
parameter:
$ms = Person::where('name', 'Foo Bar');
$persons = Person::order_by('list_order', 'ASC')->get();
$array = ['ms' => $ms, 'persons' => $persons];
return $view->with($array);
Its simple :)
<link rel="icon" href="{{ asset('favicon.ico')}}" type="image/x-icon" />