with() allows you to pass variables to a view w3coded php vs and compact() creates an array from existing w3coded php vs variables given as string arguments to w3coded php vs it.,with() is a Laravel function and compact() w3coded php vs is a PHP function and have totally different w3coded php vs purposes.,See compact() for more info on this w3coded php vs matter.,when you want to pass multiple variables w3coded php vs into a view One way to do so involves passing w3coded php vs them into the with method using an array:
Sometimes you may need to eager load several different relationships in a single operation.
To do so, you can just pass additional arguments to the with
method:
$userss = App\User::with(['name', 'email'])->get();
when you want to pass multiple variables into a view One way to do so involves passing them into the with
method using an array:
public function index()
{
$data = array('name' => 'some one',
'email' => 'someone@gmail.com',
'date' => date('Y-m-d'));
return view('welcome')->with($data);
}
You could also use multiple with methods, like so:
return view('welcome')->with('name', 'some one')->with('email','someone@gmail.com)->with('date', date('Y-m-d'));
if you needed to pass along more than two variables. You can save some typing by using PHP's compact()
function:
$name = 'some one';
$email= 'someone@gmail.com';
$date = date('Y-m-d');
return view('welcome', compact('name','email', 'date'));
or,if you needed to pass multiple arrays to a view you can use compact() function:
$array1 = ... ;
$array2 = ... ;
$array3 = ... ;
return view('welcome', compact('array1', 'array2', 'array3');
Last Update : 2023-09-22 UTC 13:31:41 PM
Last Update : 2023-09-22 UTC 13:31:21 PM
Last Update : 2023-09-22 UTC 13:31:11 PM
Last Update : 2023-09-22 UTC 13:30:57 PM
Last Update : 2023-09-22 UTC 13:30:49 PM
Last Update : 2023-09-22 UTC 13:30:18 PM
Last Update : 2023-09-22 UTC 13:30:07 PM