<bdo id='waz8T'></bdo><ul id='waz8T'></ul>

      <legend id='waz8T'><style id='waz8T'><dir id='waz8T'><q id='waz8T'></q></dir></style></legend>

      <tfoot id='waz8T'></tfoot>
      <i id='waz8T'><tr id='waz8T'><dt id='waz8T'><q id='waz8T'><span id='waz8T'><b id='waz8T'><form id='waz8T'><ins id='waz8T'></ins><ul id='waz8T'></ul><sub id='waz8T'></sub></form><legend id='waz8T'></legend><bdo id='waz8T'><pre id='waz8T'><center id='waz8T'></center></pre></bdo></b><th id='waz8T'></th></span></q></dt></tr></i><div id='waz8T'><tfoot id='waz8T'></tfoot><dl id='waz8T'><fieldset id='waz8T'></fieldset></dl></div>

      <small id='waz8T'></small><noframes id='waz8T'>

        如何从连接表中选择列:laravel eloquent

        时间:2023-09-23
      1. <i id='shbnG'><tr id='shbnG'><dt id='shbnG'><q id='shbnG'><span id='shbnG'><b id='shbnG'><form id='shbnG'><ins id='shbnG'></ins><ul id='shbnG'></ul><sub id='shbnG'></sub></form><legend id='shbnG'></legend><bdo id='shbnG'><pre id='shbnG'><center id='shbnG'></center></pre></bdo></b><th id='shbnG'></th></span></q></dt></tr></i><div id='shbnG'><tfoot id='shbnG'></tfoot><dl id='shbnG'><fieldset id='shbnG'></fieldset></dl></div>

        <small id='shbnG'></small><noframes id='shbnG'>

          <legend id='shbnG'><style id='shbnG'><dir id='shbnG'><q id='shbnG'></q></dir></style></legend>

            1. <tfoot id='shbnG'></tfoot>

                  <bdo id='shbnG'></bdo><ul id='shbnG'></ul>
                    <tbody id='shbnG'></tbody>
                • 本文介绍了如何从连接表中选择列:laravel eloquent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个与 这个不同的问题.场景相同,但我需要对结果进行更多过滤.

                  I have a different problem from this. The scenario is same but I am in need of more filtration of the results.

                  让我解释一下.

                  考虑我有 2 张桌子

                  车辆

                   id
                   name
                   staff_id
                   distance
                   mileage
                  

                  员工

                   id
                   name
                   designation
                  

                  我只想从两个表(模型)中选择 idname.Vehicle Model 包含与 Staff 模型的 belongsTo 关系.

                  I want to select only id and name from both tables(Models). The Vehicle Model contain a belongsTo relation to Staff model.

                  class Vehicle extends Model
                  {
                      public function staff()
                      {
                        return $this->belongsTo('AppStaff','staff_id');
                      }
                  }
                  

                  我加入使用这个

                  Vehicle::where('id',1)
                              ->with(['staff'=> function($query){
                                              // selecting fields from staff table
                                              $query->select(['staff.id','staff.name']);
                                            }])
                              ->get();
                  

                  当我像这样将字段放在 ->get() 中时

                  When I put fields in ->get() like this

                  ->get(['id','name'])
                  

                  它过滤了vehicle表,但没有产生Staff表的结果.

                  it filters the vehicle table but produce no result of Staff table.

                  有什么想法吗?

                  推荐答案

                  终于找到了..在 ->get() 中,你必须像这样输入 'staff_id'

                  Finally found it.. In the ->get() you have to put the 'staff_id' like this

                  Vehicle::where('id',1)
                              ->with(['staff'=> function($query){
                                              // selecting fields from staff table
                                              $query->select(['staff.id','staff.name']);
                                            }])
                              ->get(['id','name','staff_id']);
                  

                  由于我没有使用 staff_id,它无法执行连接,因此没有显示人员表字段.

                  Since I didn't take the staff_id, it couldn't perform the join and hence staff table fields were not shown.

                  这篇关于如何从连接表中选择列:laravel eloquent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel eloquent - 在加入表时防止覆盖值 下一篇:手动将项目添加到现有对象 [Laravel 5]

                  相关文章

                  最新文章

                    <small id='Rskqk'></small><noframes id='Rskqk'>

                    • <bdo id='Rskqk'></bdo><ul id='Rskqk'></ul>

                    1. <tfoot id='Rskqk'></tfoot>
                      <i id='Rskqk'><tr id='Rskqk'><dt id='Rskqk'><q id='Rskqk'><span id='Rskqk'><b id='Rskqk'><form id='Rskqk'><ins id='Rskqk'></ins><ul id='Rskqk'></ul><sub id='Rskqk'></sub></form><legend id='Rskqk'></legend><bdo id='Rskqk'><pre id='Rskqk'><center id='Rskqk'></center></pre></bdo></b><th id='Rskqk'></th></span></q></dt></tr></i><div id='Rskqk'><tfoot id='Rskqk'></tfoot><dl id='Rskqk'><fieldset id='Rskqk'></fieldset></dl></div>
                    2. <legend id='Rskqk'><style id='Rskqk'><dir id='Rskqk'><q id='Rskqk'></q></dir></style></legend>