php laravel compact() 和 ->with()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22412832/
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-08-25 04:55:00  来源:igfitidea点击:

laravel compact() and ->with()

phplaravel-4

提问by dstewart101

I have a piece of code and I'm trying to find out why one variation works and the other doesn't.

我有一段代码,我试图找出为什么一个变体有效而另一个无效。

return View::make('gameworlds.mygame', compact('fixtures'), compact('teams'))->with('selections', $selections);

This allows me to generate a view of arrays for fixtures, teams and selections as expected.

这使我能够按预期生成装置、团队和选择的数组视图。

However,

然而,

return View::make('gameworlds.mygame', compact('fixtures'), compact('teams'), compact('selections'));

does notallow the view to be generated properly. I can still echo out the arrays and I get the expected results but the view does not render once it arrives at the selections section.

不会允许产生正确的观点。我仍然可以回显数组并获得预期的结果,但是一旦到达选择部分,视图就不会呈现。

It's oké, because I have it working with the ->with()syntax but just an odd one.

没关系,因为我有它的->with()语法,但只是一个奇怪的。

Thanks. DS

谢谢。DS

回答by tliokos

The View::makefunction takes 3arguments which according to the documentation are:

View::make函数采用3 个参数,根据文档是:

public View make(string $view, array $data = array(), array $mergeData = array())

In your case, the compact('selections')is a 4thargument. It doesn't pass to the view and laravel throws an exception.

在您的情况下,这compact('selections')第四个参数。它不会传递给视图,并且 Laravel 会抛出异常。

On the other hand, you can use with()as many time as you like. Thus, this will work:

另一方面,您可以使用任意with()多的时间。因此,这将起作用:

return View::make('gameworlds.mygame')

->with(compact('fixtures'))

->with(compact('teams'))

->with(compact('selections'));

回答by j5Dev

I just wanted to hop in here and correct (suggest alternative) to the previous answer....

我只是想跳到这里并更正(建议替代方案)上一个答案....

You can actually use compact in the same way, however a lot neater for example...

您实际上可以以相同的方式使用紧凑型,但是例如更整洁...

return View::make('gameworlds.mygame', compact(array('fixtures', 'teams', 'selections')));

Or if you are using PHP > 5.4

或者,如果您使用的是 PHP > 5.4

return View::make('gameworlds.mygame', compact(['fixtures', 'teams', 'selections']));

This is far neater, and still allows for readability when reviewing what the application does ;)

这要简洁得多,并且在查看应用程序的功能时仍然具有可读性;)

回答by Jose Ortiz

I was able to use

我能够使用

return View::make('myviewfolder.myview', compact('view1','view2','view3'));

I don't know if it's because I am using PHP 5.5 it works great :)

我不知道是不是因为我使用的是 PHP 5.5 效果很好:)

回答by Nadeem Qasmi

Laravel Framework 5.6.26

Laravel 框架 5.6.26

return more than one array then we use compact('array1', 'array2', 'array3', ...)to return view.

返回多个数组然后我们compact('array1', 'array2', 'array3', ...)用来返回视图。

viewbladeis the frontend (view) blade.

viewblade是前端(视图)刀片。

return view('viewblade', compact('view1','view2','view3','view4'));

回答by Samir Lakhani

Route::get('/', function () {
    return view('greeting', ['name' => 'James']);
});
<html>
    <body>
        <h1>Hello, {{ $name }}</h1>
    </body>
</html>

or

或者

public function index($id)
{
    $category = Category::find($id);
    $topics = $category->getTopicPaginator();
    $message = Message::find(1);

    // here I would just use "->with([$category, $topics, $message])"
    return View::make('category.index')->with(compact('category', 'topics', 'message'));
}

回答by Ahmed Naguib

the best way for me :

对我来说最好的方法:

    $data=[
'var1'=>'something',
'var2'=>'something',
'var3'=>'something',
      ];
return View::make('view',$data);

回答by SWastik Thapaliya

You can pass array of variables to the compact as an arguement eg:

您可以将变量数组作为参数传递给契约,例如:

return view('yourView', compact(['var1','var2',....'varN']);

in view: if var1 is an object you can use it something like this

鉴于:如果 var1 是一个对象,你可以像这样使用它

@foreach($var1 as $singleVar1)
    {{$singleVar1->property}}
@endforeach

incase of single variable you can simply

在单个变量的情况下,您可以简单地

{{$var2}}

i have done this several times without any issues

我已经这样做了几次没有任何问题