[英]Laravel Blade Undefined Variable Error But The Variable Is Defined
This is a really odd error.这是一个非常奇怪的错误。 In my blade view:
在我的刀片视图中:
<?php //dd($site); ?>
@if($site==='site1')
On the second line @if($site==='site1')
I get a Undefined variable: site
error.在第二行
@if($site==='site1')
我得到一个Undefined variable: site
错误。 But if I uncomment out the dump and die I get the $site
variable set.但是,如果我取消注释转储并死掉,我会设置
$site
变量。 It's literally on the very next line that it tells me it doesn't have a set $site
variable in blade.它实际上在下一行,它告诉我它在刀片中没有设置
$site
变量。 Is my syntax wrong on the if
statement?我的
if
语句语法错误吗?
It looks correct to me, the documentation gives:它看起来对我来说是正确的,文档给出了:
@if (count($records) === 1)
I have one record!
@elseif (count($records) > 1)
I have multiple records!
@else
I don't have any records!
@endif
Adding a space between @if
and the first paren (
made no difference.在
@if
和第一个括号(
之间添加一个空格没有区别。
I tried clearing the view cache but that made no difference too.我尝试清除视图缓存,但这也没有什么区别。 Every other change gets registered, for example if I remove the
$
that prepends the variable name I get an undefined constant error.其他所有更改都会被注册,例如,如果我删除变量名前面的
$
,我会得到一个未定义的常量错误。 I'm developing locally.我在本地开发。
I'm at a complete loss because even though the variable is three levels deep in blade I am passing it correctly and I confirmed it with the dump and die.我完全不知所措,因为即使变量在刀片深处是三个级别,我也正确地通过了它,并且我用转储和死亡确认了它。 The conditional just won't pick it up.
有条件的只是不会拿起它。
I pass it into the view like this:我将它传递到这样的视图中:
@component('pages.common.component.mycomponent',['site'=>$site])
@endcomponent
And one more level up like this:再上一层是这样的:
@component('pages.common.parent.mycomponent',['site'=>'site1'])
@endcomponent
Any ideas I would be very grateful for!任何想法我都会非常感激!
EDIT:编辑:
The dd()
call outputs: dd()
调用输出:
"site1"
“站点 1”
As a string.作为一个字符串。
The answer was later in the 2nd level (parent to the component in which I had the error) there were TWO declarations to this third component, one of which was not passing the $site
variable.答案稍后在第二级(我有错误的组件的父级)有两个声明到第三个组件,其中一个没有传递
$site
变量。 Of course, because I was using an inline dd()
method of debugging in the third component it didn't make sense at first, as it hit the first declaration but not the second.当然,因为我在第三个组件中使用了内联
dd()
调试方法,所以起初它没有意义,因为它到达了第一个声明而不是第二个声明。
What helped me was calling the render method on the entire view and outputting it as a string, so I could see it was there in the first declaration but not in the second.帮助我的是在整个视图上调用 render 方法并将其作为字符串输出,所以我可以在第一个声明中看到它,但在第二个声明中没有。
So in the controller, instead of:所以在 controller 中,而不是:
return view('pages.routes.parent', $params);
I did:我做了:
dd(view('pages.routes.parent', $params)->render());
to get the entire view as one string output to trace where I was missing the data.将整个视图作为一个字符串 output 来跟踪我丢失数据的位置。 Inspecting the
storage
compiled views didn't help as they are partials particular to each component or view.检查
storage
编译的视图并没有帮助,因为它们是每个组件或视图特有的部分。
Note for this to work I had to wrap the @if
conditionals in another @if
conditional, checking if the $site
variable was isset()
.请注意,要使其正常工作,我必须将
@if
条件句包装在另一个@if
条件句中,检查$site
变量是否为isset()
。
Hope this can help someone else in the future.希望这可以在将来对其他人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.