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

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

        Laravel eloquent - 在加入表时防止覆盖值

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

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

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

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

                  本文介绍了Laravel eloquent - 在加入表时防止覆盖值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  class Account extends Eloquent
                  {
                    protected $table = 'account';
                  
                    /* [...] */
                  
                    public function group() {
                      return $this->belongsTo('Group');
                    }
                  }
                  

                  class Role extends Eloquent {
                  
                    protected $table = 'role';
                  
                    public function accounts() {
                      return $this->hasMany('Account');
                    }
                  
                  }
                  

                  和数据库表:accountrole

                  account
                  -------
                  id
                  name
                  role_id (nullable)
                  
                  role
                  ----
                  id
                  name
                  

                  现在的事情是:

                  我需要按 role.name 列对 accounts 进行排序.但是在 join(或 leftJoin)值被第二个表中的值覆盖后.这是一些代码:

                  And now the thing is:

                  I need to order accounts by role.name column. But after join (or leftJoin) values are overriden by those from second table. Here's some code:

                  $response = Account::with('role')->leftJoin('group', 'group.id', '=', 'account.group_id')->get();
                  

                  之后 idname 的值在 eloquent 集合中不正确.

                  After that values for id and name are incorrect in eloquent collections.

                  此外,我需要返回的是 eloquent 类型模型,因为我以 JSON 返回响应,重要的是稍后在 JS 中(解析 JSON 后)我可以只做 account.role.name.

                  Also, i need the return to be eloquent type models as i'm returning back the response in JSON, where it is important that later in JS (after parsing JSON) i can do just account.role.name.

                  更改表中字段的名称(例如:id -> account_id 和:id -> role_id)将是一种解决方法,但这不是我的情况 - 需要将主键命名为 id每张桌子.

                  Changing names of fields in tables (like: id -> account_id, and: id -> role_id) would be an workaround, but that's not my case - need to have primary key named id for every table.

                  是的,所以问题很简单:如何解决这个问题?

                  [edit] Yep, so the question is simply: how to solve that problem?

                  推荐答案

                  您可以像在普通 SQL 查询中一样使用选择":

                  You can use 'select' like you would in a normal SQL query:

                  $response = Account::with('role')
                      ->select('account.*')
                      ->leftJoin('group', 'group.id', '=', 'account.group_id')
                      ->get();
                  

                  http://laravel.com/docs/queries#selects

                  这篇关于Laravel eloquent - 在加入表时防止覆盖值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:laravel 4 雄辩的急切加载关系计数 下一篇:如何从连接表中选择列:laravel eloquent

                  相关文章

                  最新文章

                2. <small id='tMXId'></small><noframes id='tMXId'>

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

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