I have to pass multiple tables info with Laravel. I always do this:
public function get_view(){
$myinfo = Table::where('id',Auth::user()->id)->get();
return view('auth.view')->with('myinfo',$myinfo);
}
I need to pass info from other tables. The function "with" only allows me to pass a single table. Is there a way to do this?
Option 1:
return view('auth.view')->with('myinfo', $myinfo)->with('anotherVar', $anotherVar);
Option 2:
$var1, $var2;
return view('auth.view', compact('var1', 'var2'));
Which translates to (option 3):
$var1, $var2;
return view('auth.view', ['var1' => $var1, 'var2' => $var2]);
You can pass an array with multiple tables as second parameter:
public function get_view() {
$myinfo = Table::where('id', Auth::user()->id)->get();
return view('auth.view', [
'myinfo1' => $table1,
'myinfo2' => $table2,
]);
}
Also, you could use mutiple with()
There is a method compact. Your example:
public function get_view(){
$myinfo = Table::where('id',Auth::user()->id)->get();
$myinfo2 = ...
return view('auth.view', compact('myinfo', 'myinfo2'));
}