With() vs Compact() in Laravel

21,167

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.

Share:
21,167
Animesh
Author by

Animesh

Updated on July 24, 2022

Comments

  • Animesh
    Animesh almost 2 years

    Is there any difference between with() and compact() ?

    Which one is more efficient ?