Laravel 中的 With() 与 Compact()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44963889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:16:17  来源:igfitidea点击:

With() vs Compact() in Laravel

laraveldifference

提问by Animesh

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

有什么区别with()compact()

Which one is more efficient ?

哪个更有效率?

回答by baikho

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

with()是一个 Laravel 函数和compact()一个 PHP 函数,具有完全不同的用途。

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

with()允许您将变量传递给视图,并compact()从作为字符串参数给出的现有变量创建一个数组。

See compact()for more info on this matter.

有关此问题的更多信息,请参阅compact()

回答by Sagar Gautam

with()is a method made available by method from one of their classes while compact()is a method that is available by default in PHPThe with()cannot be used outside laravel but the compact()can be used anywhere in the PHPscript.

with()是一种方法提供通过方法从它们的类之一,同时compact()是默认提供的方法PHPwith()不能使用外laravel但compact()可以在任何地方在使用PHP脚本。

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.

compact()函数用于将给定的变量转换为数组,其中数组的键将是变量的名称,数组的值将是变量的值。

回答by mohammadreza khalifeh

compact is php function :

紧凑是php函数:

compact— Create array containing variables and their values

compact— 创建包含变量及其值的数组

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

compact()采用可变数量的参数。每个参数可以是string包含array变量名称的一个,也可以是包含变量名称的一个。该数组可以在其中包含其他变量名数组;compact() 递归处理它。

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

with()Eloquent 模型上的方法使您能够快速加载。

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 withmethod:

有时您可能需要在单个操作中预先加载多个不同的关系。为此,您只需将其他参数传递给该with方法:

$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 withmethod using an array:

当您想将多个变量传递到视图中时,一种with方法是使用数组将它们传递到方法中:

public function index()
{

    $data = array('name' => 'some one',
                  'email' => '[email protected]',
                  'date' => date('Y-m-d'));  

    return view('welcome')->with($data);

}

You could also use multiple with methods, like so:

您还可以使用多个 with 方法,如下所示:

return view('welcome')->with('name', 'some one')->with('email','[email protected])->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:

如果您需要传递两个以上的变量。您可以使用 PHP 的compact()函数来节省一些输入:

$name = 'some one';
$email= '[email protected]';
$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:

或者,如果您需要将多个数组传递给视图,您可以使用 compact() 函数:

$array1 = ... ;
$array2 = ... ;
$array3 = ... ;
return view('welcome', compact('array1', 'array2', 'array3');