[英]Two relationships between two same models/tables
Hello, I'm having some trouble trying to make work two relationships between the two models (Laravel 5.5): Order
and User
. 您好,我在尝试使两个模型(Laravel 5.5)之间建立两个关系时遇到麻烦:
Order
和User
。
1-----m | 1 -----米| A
User
(as a customer) can make many Order
s User
(作为客户)可以发出多个Order
1-----m | 1 -----米| A
User
(as an employee) can take many Order
s User
(作为员工)可以接受多个Order
So, in my orders
table I have user_id
and employee_id
as foreign keys. 因此,在我的
orders
表中,我将user_id
和employee_id
作为外键。
Now, some orders doesn't have an initial employee_id
because orders need to be accepted, then I set up that value. 现在,某些订单没有初始的
employee_id
因为需要接受订单,然后设置该值。
Using tinker, I've located this Order
object (id:30), it has a user_id
and a employee_id
but when I dd()
it, this returns: 使用修补匠,我找到了这个
Order
对象(id:30),它有一个user_id
和一个employee_id
但是当我dd()
时,它返回:
=> App\Models\Order {#882
order_id: 30,
user_id: 7, // <-- foreign key
employee_id: 6, // <-- foreign key
restaurant_id: 4, // <-- foreign key (for other table)
// ...
total: "120.00",
created_at: "2018-01-31 00:15:41",
updated_at: "2018-01-31 00:15:41",
deleted_at: null,
restaurant: App\Models\Restaurant {#900 // <- MY RESTAURANT RELATION (M-1)
restaurant_id: 4,
name: "Stark, Padberg and Buckridge",
// ...
// ...
created_at: "2018-01-12 18:18:17",
updated_at: "2018-01-12 18:18:17",
deleted_at: null,
},
customer: App\Models\User {#914 // <- MY CUSTOMER RELATION (M-1)
user_id: 7,
restaurant_id: 4,
// ...
created_at: "2018-01-30 17:52:33",
updated_at: "2018-01-30 23:32:00",
deleted_at: null,
},
deliverer: null, // <-- THIS SHOULD CONTAIN THE EMPLOYEE DATA
// THAT BELONGS TO THE DELIVERER RELATION
}
What am I doing wrong? 我究竟做错了什么?
Information of my logic. 我的逻辑信息。
In my Order
model I've defined the relationships (among other ones) as follows (all my primary keys follow this convention: model_id
): 在我的
Order
模型中,我已经定义了其他关系(我的所有主键都遵循这个约定: model_id
):
/**
* Return the user who created the order.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function customer()
{
return $this->belongsTo(
'App\Models\User',
'user_id',
'user_id'
);
}
/**
* Return the employee (user) who deliver the order.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function deliverer()
{
return $this->belongsTo(
'App\Models\User',
'employee_id',
'user_id'
);
}
/**
* A customer can make many orders
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function ordersMade()
{
return $this->hasMany('App\Models\Order',
'user_id',
'user_id'
);
}
/**
* An employee attends many orders.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function ordersDelivered()
{
return $this->hasMany('App\Models\Order',
'employee_id',
'user_id'
);
}
This is the schema of my orders
table: 这是我的
orders
表的架构:
This is the schema of my users
table: 这是我的
users
表的架构:
Yes, I have users with ids 6
and 7
: 是的,我有ID为
6
和7
用户:
In Tinker i've done: 在Tinker中,我已经完成了:
>>> $o = App\Models\Order::find(30);
>>> $o->customer;
>>> $o->restaurant;
>>> $o->deliverer;
>>> $o;
Well, there wasn't any problem with my code. 好吧,我的代码没有任何问题。 I have the
softDeletes
set to true
on my models, so I was trying to get a property of a "deleted" element. 我在模型
softDeletes
设置为true
,因此我试图获取“已删除”元素的属性。
As simple as that. 就如此容易。 Thanks to @MahdiYounesi, who made me check that.
感谢@MahdiYounesi,他让我检查了一下。
although the problem was almost a stupid one, is a good remainder to note that when you need to also return softDeleted
records when querying from an entity, you could just -as the docs says- add withTrashed()
in each query that needs this kind of results, or directly in the model when defining the relationship like this: 尽管该问题几乎是一个愚蠢的问题,但值得注意的是,当您从实体查询
softDeleted
需要返回softDeleted
记录时,您可以-正如文档 withTrashed()
-在需要这种查询的每个查询中添加withTrashed()
的结果,或者在定义如下关系时直接在模型中:
/**
* Return the user who created the order.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function customer()
{
return $this->belongsTo(
'App\Models\User',
'user_id'
)->withTrashed();
}
/**
* Return the employee (user) who deliver the order.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function deliverer()
{
return $this->belongsTo(
'App\Models\User',
'employee_id',
'user_id'
)->withTrashed();
}
This will collect the "deleted" data, usefull for historical information for example. 这将收集“已删除”数据,例如对历史信息有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.