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 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料