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

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

      1. 如何在 Eloquent 中为列的名称设置别名

        时间:2023-09-24

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

              <tbody id='81Nvz'></tbody>
            <legend id='81Nvz'><style id='81Nvz'><dir id='81Nvz'><q id='81Nvz'></q></dir></style></legend>

          1. <small id='81Nvz'></small><noframes id='81Nvz'>

              <bdo id='81Nvz'></bdo><ul id='81Nvz'></ul>

                • <tfoot id='81Nvz'></tfoot>
                  本文介绍了如何在 Eloquent 中为列的名称设置别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个名为 Eloquent 的 eloquent 模型:

                  I have an eloquent model named Eloquent:

                  Products::where("actice", "=", true)->get()->toArray();
                  

                  现在我想给它添加join语句,我已经定义了一个scopeQuery:

                  Now I want to add join-statement to it, I have defined a scopeQuery with:

                  public function scopeJoinWithTags($query)
                      {
                          return $query->leftJoin("tags", "tags.id", "=", "products.tag_id");
                      }
                  

                  然后我们的主要查询更改为:

                  Then our main query changes to:

                  Products::where("actice", "=", true)->joinWithTags->get()->toArray();
                  

                  我得到的没问题,这正是我所期望的,但我想将标签表的名称属性更改为 tag_name,我该怎么做?我的意思是,我在查询中的某处说:

                  What I get is OK, it is what I do expect, but I want to change the name property of tags table to tag_name, how should I do that? I mean, i say somewhere in my query to:

                   tags.name AS tag_name
                  

                  所以在最终结果数组中我这样做:

                  So that in the final result array I do :

                  $result[$i]['tag_name'];
                  

                  虽然现在我必须:

                  $result[$i]['name'];
                  

                  推荐答案

                  最简单的方法是将您需要的字段添加到 get() 方法中,并为您想要的字段添加别名在那里重命名.

                  Simplest way to do this would be to add the fields you need to the get() method and alias the ones you want to rename there.

                  Products::where("actice", "=", true)
                      ->joinWithTags
                      ->get(['tags.name AS tag_name', 'products.*'])
                      ->toArray();
                  

                  这篇关于如何在 Eloquent 中为列的名称设置别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何使用 Laravel Eloquent 创建子查询? 下一篇:按多列对 Eloquent 集合进行排序的语法是什么?

                  相关文章

                  最新文章

                • <legend id='dB8h0'><style id='dB8h0'><dir id='dB8h0'><q id='dB8h0'></q></dir></style></legend>

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

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

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