Laravel compact() and ->with()


Last Update : 2023-09-22 UTC 09:41:23 AM

Answers of > Laravel compact() and ->with()

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' => '',
                  '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','>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= '';
$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');

Current topics : Laravel compact() and ->with()

Newly Added Questions

Similar Questions

Questions :

How To Group Array Key Value

Last Update : 2023-09-22 UTC 13:31:53 PM

Questions :

PhpStorm Warning For React Attributes In Jsx File With SCSS File

Last Update : 2023-09-22 UTC 13:31:41 PM

Questions :

Why Is The File Not Showing Up In Request.files And In Request.forms Instead?

Last Update : 2023-09-22 UTC 13:31:21 PM

Questions :

Proxying Assets From React App Directory In Slim Framework?

Last Update : 2023-09-22 UTC 13:31:11 PM

Questions :

Laravel 5.4 Can't Run “php Artisan Preset React” Comand

Last Update : 2023-09-22 UTC 13:30:57 PM

Questions :

How To Update Session Values Without Signing Out?

Last Update : 2023-09-22 UTC 13:30:49 PM

Questions :

Array Is Not Visible

Last Update : 2023-09-22 UTC 13:30:35 PM

Questions :

React Routing For Login Using Symfony

Last Update : 2023-09-22 UTC 13:30:27 PM

Questions :

Sanctum With React SPA Returning 419 Page Expired

Last Update : 2023-09-22 UTC 13:30:18 PM

Questions :

How Do I Import An Input String Into Another Page

Last Update : 2023-09-22 UTC 13:30:07 PM

© 2023 W3CODED - All Rights Reserved.