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

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

    1. <legend id='RfDeB'><style id='RfDeB'><dir id='RfDeB'><q id='RfDeB'></q></dir></style></legend>
      1. <tfoot id='RfDeB'></tfoot>
        • <bdo id='RfDeB'></bdo><ul id='RfDeB'></ul>

        如何在 Laravel 5.3 中创建两次连接到表的查询?

        时间:2023-09-23

          <tbody id='FPEt7'></tbody>
          • <small id='FPEt7'></small><noframes id='FPEt7'>

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

                <tfoot id='FPEt7'></tfoot>
                  <bdo id='FPEt7'></bdo><ul id='FPEt7'></ul>
                • <legend id='FPEt7'><style id='FPEt7'><dir id='FPEt7'><q id='FPEt7'></q></dir></style></legend>
                  本文介绍了如何在 Laravel 5.3 中创建两次连接到表的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我需要通过一个查询获得两个城市名称:

                  I need get two city names with one query:

                  例如:

                  城市表:

                  +---------+----------+
                  |  Pana   |   Name   |
                  +---------+----------+
                  |   THR   |  Tehran  |
                  |   LON   |  London  |
                  +---------+----------+
                  

                  在模型中:from_cityTHRto_cityLON

                  In Model: from_city is THR and to_city is LON

                  public function scopePrintQuery($query, $id)
                  {
                      $join = $query
                          -> join('cities', 'cities.pana', 'flights.from_city')
                          -> join('cities', 'cities.pana', 'flights.to_city')
                          -> where('flights.id', $id)
                          ->get([
                              'flights.*',
                              'cities.name as from_city'
                              ??? for to_city?
                          ]);
                      return $join;
                  }
                  

                  现在,我需要在此查询中获取 from_city 名称和 to_city 名称.

                  Now, I need get from_city name and to_city name in this query.

                  该查询不适用于一张表中的两个连接!

                  The query does not work with two joins from one table!

                  如何创建这个查询?

                  推荐答案

                  使用直接的 SQL 你可以给每个连接的表一个别名 - 例如

                  With straight SQL you could give each joined table an alias - e.g.

                  SELECT flights.*
                  FROM flights as f
                   JOIN cities as fromCity on fromCity.pana = f.from_city
                   JOIN cities as toCity on toCity.pana = f.to_city
                  WHERE f.id = 3 --
                  

                  对于 Eloquent,使用 select() 来指定选择字段.还可以使用 DB::raw() 来使用原始 SQL(例如给出一个表的别名,如 DB::raw('cities as toCity').

                  With Eloquent, use select() to specify select fields. Also use DB::raw() to use raw SQL (e.g. giving an alias to table like DB::raw('cities as toCity').

                  public function scopePrintQuery($query, $id)
                  {
                    $join = $query
                      -> join(DB::raw('cities as fromCity'), 'fromCity.pana', 'flights.from_city')
                      -> join(DB::raw('cities as toCity'), 'toCity.pana', 'flights.to_city')
                      -> where('flights.id', $id)
                      ->select([
                          'flights.*',
                          DB::raw('fromCity.name as from_city')
                          DB::raw('toCity.name as to_city')
                      ]);
                      return $join->get();
                  }
                  

                  这篇关于如何在 Laravel 5.3 中创建两次连接到表的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel Eloquent 使用 Where In? 查询 JSON 列 下一篇:Laravel 身份验证与雄辩的角色驱动程序

                  相关文章

                  最新文章

                    1. <tfoot id='xiyMd'></tfoot>
                        <bdo id='xiyMd'></bdo><ul id='xiyMd'></ul>

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

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

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