Home > Net >  Pass Array to Laravel view
Pass Array to Laravel view

Time:09-28

return view('price')->with('day',$day)->with('values',$values);

I tried to pass $day, $values arrays to view.

dd($day); 

gives

array:3 [▼
  0 => "2021-09-06 18:48:34"
  1 => "2021-09-10 09:59:22"
  2 => "2021-09-28 09:58:02"
]

dd($values)

is in similar format. but when I pass to view (price.blade.php) it says undefined offset[1]

CodePudding user response:

I'm not sure that you can chain "with" on views - I think that the second "with" in your code over-writes the first "with", so when you come to use $day in your view, it's not defined.

Where you want to pass multiple variables to a view, rather than :

return view('price')->with('day',$day)->with('values',$values);

you should use one "with", passing the variables as an array :

return view('price')->with(['day' => $day, 'values' => $values]);

Where you are giving the variables the same name in the view as they had in the controller, then you can save yourself more time by using the 'compact' function :

return view('price')->with(compact('day', 'values');

And it will automatically pass through $day as 'day', and $values as 'values'.

CodePudding user response:

Try this:

return view('price', compact('day', 'values'));

Omit the ->with and make sure that you have declared $day and values

  • Related