简体   繁体   English

laravel compact() 和 ->with()

[英]laravel compact() and ->with()

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 not allow 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

The View::make function takes 3 arguments 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 4th argument.在您的情况下, compact('selections')第四个参数。 It doesn't pass to the view and laravel throws an exception.它不会传递给视图,并且 Laravel 会抛出异常。

On the other hand, you can use with() as many time as you like .另一方面,您可以根据需要多次使用with() Thus, this will work:因此,这将起作用:

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

->with(compact('fixtures'))

->with(compact('teams'))

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

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 ;)这要简洁得多,并且在查看应用程序的功能时仍然具有可读性;)

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 效果很好:)

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', ...)返回视图。

viewblade is the frontend (view) blade. viewblade是前端(视图)刀片。

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

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 scalar variable you can simply标量变量的情况下,您可以简单地

{{$var2}}

i have done this several times without any issues我已经这样做了几次没有任何问题

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'));
}

the best way for me :对我来说最好的方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM