我想根據條件從模型中分離值。我想從型別為 3 的組織中分離出所有人員,即工人。但是當我嘗試這樣做時,它會分離所有人,而不考慮 where 條件。
這是我的人際關系:
類別:組織
public function people()
{
return $this->belongsToMany(People::class, 'organization_people')
}
public function workers()
{
return $this->people()->where('type', 3); //returns all workers
}
public function detachWorkers()
{
$this->workers()->detach(); // this detaches all people, I want it to detach only workers
}
uj5u.com熱心網友回復:
你需要這樣做:
public function people()
{
return $this->belongsToMany(People::class, 'organization_people')
}
public function workers()
{
return $this->people()->where('type', 3);
}
public function detachWorkers()
{
$this->people()->detach($this->workers);
}
IIUC Laravel 不會將該約束附加where
到detach
. 但是,它將附加您直接放在資料透視表上的任何約束,例如$this->people()->wherePivot('type', 3);
假設您type
在資料透視表上有一個列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/450828.html
上一篇:Laravel8.83.2目標[Illuminate\Contracts\Bus\Dispatcher]不可實體化