gpt4 book ai didi

php - 查询获取人员相关(数据透视表)

转载 作者:行者123 更新时间:2023-11-29 09:39:16 25 4
gpt4 key购买 nike

我有这个模型:

STAFF
-----
id
name


PAGE_STAFF
----------
page_id
staff_id


PAGE
----
id
title
parent_id



PARENT_PAGE
-----------
id
title

像这样的关系方法:

     in Staff Model
/**
* The areas that belongs to the staff member.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function pages()
{
return $this->belongsToMany(Page::class);
}


in Page Model
/**
* The staff that belongs to the page.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/

public function staff()
{
return $this->belongsToMany(Staff::class);
}

PARENT_PAGE 包含“主页”的 id(和标题)。该模型不与任何模型连接,我用它来过滤事件。我的意思是我只需要 page_staff 中的 page_id,parent_page.id == page_staff.page_id。

但我需要知道数据透视表“page_staff”中的条目。需要的查询是

“page_staff”模型中包含的所有工作人员 (page_staff.staff_id) 且 page_staff.page_id == Parent_page.id。

此后我需要所有员工模型(staff.id)

如何在 Eloquent 中执行此操作?

致以诚挚的问候

最佳答案

对 ParentPage 在这一切中如何连接有点困惑。要获取数据透视表中的所有人员,我们可以使用 has() 来完成。当我知道父页面在此域中的位置时,将更新问题。

$staffWithPages = Staff::whereHas('pages.parent', function ($query) {
$query->where('laravel_reserved_0.id', '!=', 'page_staff.page_id');
})->get();

如果您想要 id,请使用 Laravel Collection pluck 方法。

$staffWithPages->plukc('id')->all();

关于php - 查询获取人员相关(数据透视表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56964775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com