简体   繁体   English

Laravel foreach给出未定义的错误

[英]Laravel foreach giving undefined error

In my laravel view, I have a select list built up of 3 foreachs. 在我的laravel视图中,我有一个包含3个foreach的选择列表。 I appreciate that the query in the 2nd foreach isn't best practice but I'm unaware of how best to construct this. 我很欣赏第二个foreach中的查询不是最佳实践,但我不知道如何最好地构造它。

My issue at the moment is largely that I am getting an undefined error referring to building, in the 2nd foreach. 目前,我的问题主要是在第二个foreach中,我遇到了一个关于建筑的未定义错误。

Can anyone help? 有人可以帮忙吗?

@foreach($buildings as $building)
   <optgroup label="{{ Str::upper($building->title) }}"></optgroup>
    @foreach (DB::select('select id, description from `floors` where `id` in (select distinct `floor_id` from `rooms` where `building_id` = $building->id)') as $floor)
       <optgroup label="{{ $floor->description }}"></optgroup>
          @foreach (Room::where('active',1)->where('floor_id', $floor->id)->where('building_id', $building->id)->orderBy('name')->get() as $room)
          <option value="{{ $room->id }}"> - {{ $room->fulltitle }}</option>
          @endforeach
    @endforeach
@endforeach

You're using ' -quoted strings. 您正在使用'引号的字符串。 They do NOT interpolate variables. 他们插值变量。

$foo = 'bar';

echo '$foo'; // outputs $, f, o, o
echo "$foo"; // outputs b, a ,r

This means you're sending a literal $ , b , u , etc... to the database, which will be an unknown/illegal field name. 这意味着您要向数据库发送原义的$bu等,这将是未知/非法的字段名。

So you should have: 因此,您应该具有:

@foreach (DB::select("select [...snip...] = $building->id)") as $floor)
                     ^------------------------------------^

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

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