Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Pass more than one variable to view

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 modify the return $view->with('persons', $persons); line to also pass the $ms variable to the view?

    function view($view)     {         $ms = Person::where('name', 'Foo Bar');          $persons = Person::order_by('list_order', 'ASC')->get();          return $view->with('persons', $persons);     } 
like image 434
pjmil Avatar asked Nov 21 '13 01:11

pjmil


People also ask

How to pass multiple parameters with view in Laravel?

If your using laravel, try this: return view('app-student. app-student dashboard',compact('education_level_data','student_data',.......n)); where by: 1. education_level_data----first-parameter 2. student_data ----second parameter NB: And you can pass more parameters as much as you can.

How can a function return multiple values in laravel?

Possible solution is that you define private/protected variables as a and b within the class and in the method totalStocks() you can set values to them. So you can access $this->a and $this->b as properties of the class whenever class is in use. Save this answer. Show activity on this post.

How pass data from model to controller in laravel?

You have to create the method in controller class as a static method and you can directly call the controller's method by specifying the full path of controller in view file. You can also pass data from view to controller using anchor tag with route method and get in controller's method.


2 Answers

Just pass it as an array:

$data = [     'name'  => 'Raphael',     'age'   => 22,     'email' => '[email protected]' ];  return View::make('user')->with($data); 

Or chain them, like @Antonio mentioned.

like image 162
rmobis Avatar answered Sep 28 '22 01:09

rmobis


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'] }} 
like image 29
Antonio Carlos Ribeiro Avatar answered Sep 28 '22 01:09

Antonio Carlos Ribeiro