简体   繁体   English

在Laravel视图中访问嵌套数组

[英]Accessing nested array's in Laravel view

Controller Code: 控制器代码:

    public function claims($id)
    {
        $claims = Claim::whereBetween('created_at', [
                            '2016-03-01', 
                            '2016-03-31'
                        ])->get();

        return View::make('pdfs.view', $claims);

    }

In my view I'm getting a message that $claims is an undefined variable. 在我看来,我收到一条消息, $claims是一个未定义的变量。

I know that with a single array I can simply access the array properties by callig a variable of the same name. 我知道只有一个数组,我可以通过callig一个同名变量来简单地访问数组属性。 ie $claims['id] would simply by $id $claims['id]将简单地由$ id

However I cannot do this with a multidimensional array, as $claims does not exist 但是我不能使用多维数组来执行此操作,因为$claims不存在

Also, I cannot pass the data as an object using ->with('claims' $claims) as I'm generating a PDF and the library does not support that function. 另外,由于我正在生成PDF,并且库不支持该功能,因此无法使用->with('claims' $claims)将数据作为对象传递。

Any ideas how I can access the data? 有什么想法可以访问数据吗?

Because your array doesn't contains that key 因为您的阵列不包含该key

return View::make('pdfs.view', $claims);

instead you can use compact like as 相反,您可以使用compact等作为

return View::make('pdfs.view', compact('claims'));

Or you need to do it somewhat assigning your values to same key like as 或者您需要这样做,将值分配给相同的键,例如

$claims['claims'] = Claim::whereBetween('created_at', [
                        '2016-03-01', 
                        '2016-03-31'
                    ])->get();
return View::make('pdfs.view', $claims);

or you can simply use Laravels way using with variable like as 或者您可以简单地使用Laravels方式with变量一样使用

return View::make('pdfs.view')->withClaims($claims);

Note : While using compact make sure your variable name must matches your string 注意:使用compact确保您的变量名必须与字符串匹配

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

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