Ok, so i have following two models: Account
and Role
class Account extends Eloquent
{
protected $table = 'account';
/* [...] */
public function group() {
return $this->belongsTo('Group');
}
}
and
class Role extends Eloquent {
protected $table = 'role';
public function accounts() {
return $this->hasMany('Account');
}
}
and database tables: account
and role
account
-------
id
name
role_id (nullable)
role
----
id
name
And now the thing is:
我要订accounts
*role.name
栏.但after join(或leftJoin)值被第二个表中的值覆盖.以下是一些代码:
$response = Account::with('role')->leftJoin('group', 'group.id', '=', 'account.group_id')->get();
After that values for id
and name
are incorrect in eloquent collections.
另外,我需要返回Eloquent 的类型模型,因为我要用JSON返回响应,在JS中稍后(在解析JSON之后)我只能返回account.role.name
,这一点很重要.
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.
[编辑]