使用laravel雄辩时,如何获取具有相同名称的所有列? [英] How to get all column with some having same name when using laravel eloquent?

查看:76
本文介绍了使用laravel雄辩时,如何获取具有相同名称的所有列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说我有2个带有这样的列的表:

Let say I have 2 tables with column like this:

table_a: (with a corresponding model TableA)
id | (20+ other field) | deleted_at | created_at | updated_at

table_b: (with a corresponding model TableB)
id | a_id | ( 30+ other field ) | deleted_at | created_at | updated_at

现在我正在使用laravel并加入这两个表

Now I am using laravel and join on this two table

$result = TableA::join('table_b', 'table_a.id', '=', 'table_b.a_id')
    ->where('table_b.created_at', '>=', date('Y-m-d'))
    ->get();

问题是,当列名与table_a相同时,我无法访问table_b的某些字段. (即iddeleted_atcreated_atupdated_at)

The problem is that I am not able to some field of table_b when the column name are the same as table_a. (i.e. id, deleted_at, created_at and updated_at)

我已经进行了一些搜索,并从此问题开始stackoverflow ,我可以这样做:

I have done some search, and from this question on stackoverflow, I may do:

$result = TableA::join('table_b', 'table_a.id', '=', 'table_b.a_id')
    ->where('table_b.created_at', '>=', date('Y-m-d'))
    ->select('table_b.id as b_id', 'table_b.created_at as b_created_at')
    ->get();

但是,当使用此方法时,我需要将所有列名添加到select语句中,并且要在50多个字段上执行此操作很麻烦.它是导出功能,因此所有字段都是必需的.有没有一种方法可以进行必要的重命名而无需列出不需要重命名来检索所有行的所有其他字段?还是有一种方法可以检索所有行而无需重命名?

However, when using this method, I need to add all the column name into the select statement, and it is painful to do that on 50+ fields. It is an exporting function so all fields are necessary. Is there a way I can make the necessary rename without listing all other field that don't need a rename to retrieve all the rows? Or is there a way I can retrieve all the row without a rename?

P.S.我正在使用laravel4.这是一个旧项目,更新比列出所有字段还要痛苦.

P.S. I am using laravel 4. This is an old project and updating is even more painful than listing all fields.

推荐答案

不幸的是,雄辩的查询不会为联接表中的字段加上别名,因此具有相同名称的列会相互覆盖.

Unfortunately Eloquent queries do not alias the fields from joined tables so the columns with the same name overwrite each other.

显而易见的解决方案是重命名列,但这并不总是可行的选择.

The obvious solution would be to rename the columns which is not always a viable option.

另一种解决方案是摆脱联接,并使用查询构建器的 whereHas 方法,该方法允许根据相关模型中的数据过滤数据.由于它使用子选择而不是联接,因此不会覆盖任何列.

Another solution would be getting rid of joins and use whereHas method of a query builder, that allows filtering data based on data in related models. As it uses subselects instead of joins, no columns get overwritten.

您需要做的就是在您的 TableA 模型中使用以下方式定义一个关系 table_b :

All you'd need to do is defining a relation table_b in your TableA model with:

class TableA extends Model {
  public function table_b() {
    return $this->hasOne(TableB::class); //or hasMany, depending on your data model
  }
}

并替换:

$result = TableA::join('table_b', 'table_a.id', '=', 'table_b.a_id')
  ->where('table_b.created_at', '>=', date('Y-m-d'))
  ->get();

使用

$result = TableA::whereHas('table_b', function($query) {
  $query->where('created_at', '>=', date('Y-m-d');
});

这将为您提供所有具有与给定条件匹配的相关TableB记录的TableA记录.

That would give you all TableA records that have a related TableB record that matches given criteria.

这篇关于使用laravel雄辩时,如何获取具有相同名称的所有列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆