With() vs Compact() in Laravel
Solution 1
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.
See compact() for more info on this matter.
Solution 2
with()
is a method made available by method from one of their classes while compact()
is a method that is available by default in PHP
The with()
cannot be used outside laravel but the compact()
can be used anywhere in the PHP
script.
The compact()
function is used to convert given variable to to array in which the key of the array will be the name of the variable and the value of the array will be the value of the variable.
Animesh
Updated on July 24, 2022Comments
-
Animesh almost 2 years
Is there any difference between
with()
andcompact()
?Which one is more efficient ?