【问题标题】:laravel compact() and ->with()laravel compact() 和 ->with()
【发布时间】:2014-04-20 04:57:31
【问题描述】:

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

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

这让我可以按预期生成赛程、球队和选择的数组视图。

然而,

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

不允许正确生成视图。我仍然可以回显数组并获得预期的结果,但视图在到达选择部分后不会呈现。

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

谢谢。 搜索引擎

【问题讨论】:

    标签: php laravel-4


    【解决方案1】:

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

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

    在您的情况下,compact('selections')4th 参数。它不会传递给视图,并且 laravel 会抛出异常。

    另一方面,您可以使用with() 任意次数。因此,这将起作用:

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

    【讨论】:

    • ->with(compact('fixtures', 'teams', 'selections'))
    • return view('gameworlds.mygame',compact('fixtures','teams','selections'));
    • return view('gameworlds.mygame', ["fixtures"=>$fixtures,...]);
    【解决方案2】:

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

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

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

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

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

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

    【讨论】:

      【解决方案3】:

      我可以使用

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

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

      【讨论】:

        【解决方案4】:
        Route::get('/', function () {
            return view('greeting', ['name' => 'James']);
        });
        <html>
            <body>
                <h1>Hello, {{ $name }}</h1>
            </body>
        </html>
        

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

        【讨论】:

          【解决方案5】:
          $data = [
             'var1' => 'something',
             'var2' => 'something',
             'var3' => 'something',
          ];
          
          return View::make('view', $data);
          

          【讨论】:

            【解决方案6】:

            Laravel 框架 5.6.26

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

            viewblade 是前端(视图)刀片。

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

            【讨论】:

            • 请在您的代码中添加说明。纯代码解决方案毫无用处。
            【解决方案7】:

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

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

            在视图中: 如果 var1 是一个对象 你可以像这样使用它

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

            如果是标量变量,您可以简单地

            {{$var2}}
            

            我已经做了好几次了,没有任何问题

            【讨论】:

            • 在 return view('yourView', compact(['var1','var2',....'varN']); 中,你最后错过了一个 ')'。跨度>
            猜你喜欢
            • 2017-12-11
            • 2015-04-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-06-15
            • 1970-01-01
            • 2021-02-03
            • 2017-11-05
            相关资源
            最近更新 更多