DEV Community

Youcef  Benabderrezak
Youcef Benabderrezak

Posted on

Compact() vs With() in Laravel

with() is a Laravel function and compact() is a PHP function and have totally different purposes.

with() allows you to pass variables to a view and compact() creates an array from existing variables given as string arguments to it

or

compact() takes a variable number of parameters. Each parameter can be either a string containing the name of the variable, or an array of variable names. The array can contain other arrays of variable names inside it; compact() handles it recursively.

Method with() on Eloquent model enables for you eager loading.

src=
1/ https://www.php.net/manual/en/function.compact.php
2/ https://stackoverflow.com/questions/44963889/with-vs-compact-inlaravel

Top comments (0)