【发布时间】:2022-02-03 10:01:35
【问题描述】:
我有一个 Employee 模型,它有一个通过 belongsToMany 关系 agents() 访问的数据透视表 customer_employee(employee_id、customer_id、token) :
class Employee extends Model {
...
public function agents(): BelongsToMany
{
return $this->belongsToMany(Employee::class)->withPivot('token');
}
我想查询(在 Eloquent 中)de 数据库以获取其代理令牌 == 'abc' 的 Employee 模型。
谁能帮帮我?谢谢和问候。
(1) token 和 (employee_id, customer_id) 是唯一的
(2) 在原始 sql 中是
$sql = 'select employee.*
from employee inner join customer_employee
on employee.id = customer_employee.employee_id
where customer_employee.token = "abc"';
【问题讨论】:
-
您好,请在文档中阅读#Has One Through 和#Has Many Through。那应该有帮助。 laravel.com/docs/8.x/eloquent-relationships#has-one-through
标签: laravel eloquent relationship