How to pass variables to view in Laravel? It’s quite easy.
1. with(‘xxx’, $xxx)
In controller:
$name = "Tom";
$address = "XXX Rd.";
:
Return view('name')->with('name', $name)->with('address', $address);
In view:
{{ $name }}
2. Pack all variables into an array
In controller:
$user = [
$name => "Tom";
$address => "XXX Rd.";
]
return view('products.cart', $user);
In view:
{{ $name }}
3. compact
Using compact
is a more concise and recommending way to do so.
In controller:
$cart = session()->get('cart');
$total = 0;
:
return view('products.cart', compact('cart', 'total'));
In view:
{{ $cart }}
{{ $total }}
No comments:
Post a Comment