duanmei1922 2014-11-25 19:41
浏览 45
已采纳

Laravel foreach给出了未定义的错误

In my laravel view, I have a select list built up of 3 foreachs. I appreciate that the query in the 2nd foreach isn't best practice but I'm unaware of how best to construct this.

My issue at the moment is largely that I am getting an undefined error referring to building, in the 2nd 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
  • 写回答

1条回答 默认 最新

  • duanhuanzhi6431 2014-11-25 19:46
    关注

    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.

    So you should have:

    @foreach (DB::select("select [...snip...] = $building->id)") as $floor)
                         ^------------------------------------^
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal