James Sachi
James Sachi

Reputation: 71

Passing Multiple Variable into a view

How do I pass a multiple variable into a view and how do I get it?

supposing i have a two variable

$dateFrom = 2016-02-22
$dateTo = 2016-02-25

am i doing right with my route?
 return View('admin.inventory-bloodCollectionView1')->with($dateFrom)->with($dateTo);   
and how do i get it from the view?

Upvotes: 1

Views: 98

Answers (4)

Abdul Basit
Abdul Basit

Reputation: 374

You can also pass in the form of array just like this.

return view('admin.inventory-bloodCollectionView1')
   ->with(array('fromDate' => $dateFrom, 'toDate'=> $dateTo));

These variables will be available in view as $fromDate and $toDate.

Upvotes: 0

Brandon Smith
Brandon Smith

Reputation: 240

Another convenient way to pass variables is using compact.

return View('admin.inventory-bloodCollectionView1', compact('dateFrom', 'dateTo'));

Upvotes: 1

user2229824
user2229824

Reputation: 21

You can pass multiple variables or items in to the view by supplying an array to the view method. For example :

return view('view_name', ['value_name' => value , 'value_name2' => value2 ]);  

U can use the items in the view now using : $value_name and $value_name2 respectively.

Upvotes: 1

Ohgodwhy
Ohgodwhy

Reputation: 50787

You need to alias the name of the variable as the first, argument, like so:

return View('admin.inventory-bloodCollectionView1')
   ->with('dateFrom', $dateFrom)
   ->with('dateTo', $dateTo);

Now they are available in the view as $dateTo and $dateFrom.

Upvotes: 1

Related Questions