我今天遇到了一個簡單的問題。我在 Livewire 組件中作業,我有一個物件集合,當我單擊一個物件時,我想將它添加到一個陣列(有效)中,然后想將它從集合中洗掉 - 這樣該物件就不是可點擊了。我有的:
public function addPartToUsage($partId){
$partToAdd = Part::where('id', '=', $partId)->first();
$this->verwendung[] = ['id'=> $partToAdd->id, 'partBezeichnung'=>$partToAdd->id.':'.$partToAdd->zeichnungsnummer.' - '.$partToAdd->artikelbezeichnung];
$this->partsClient = Part::where('artikelbezeichnung', 'LIKE', '%' . $this->inputsearchpart . '%')
->orWhere('zeichnungsnummer', 'LIKE', '%' . $this->inputsearchpart . '%')
->orderBy('artikelnummer')
->get();
foreach ($this->verwendung as $item) {
if($this->partsClient->contains('id', $item['id'])){
$this->partsClient->forget($this->partsClient->where('id', $item['id'])->id);
} } }
但它不起作用,因為我不知道包含匹配 id 的元素的鍵。
$this->verwendung 是我的陣列,其中包含 $this->verwendung[0]['id'] 中的 id,而 $this->partsClient 是我來自資料庫的集合。
如何修改集合而不將其放入陣列,然后檢查匹配的 id,然后重新收集?或者更好的是如何檢索集合的匹配鍵,以便我可以使用 Forgot() ?感謝這里的所有人!
uj5u.com熱心網友回復:
而不是檢查該ID是否包含在集合中,你可以只是過濾它reject()直接
foreach ($this->verwendung as $item) {
$this->partsClient->reject(function ($value, $key) use ($item) {
return $value->id == $item['id'];
});
}
或者干脆
$verwendungCollection = collect($this->verwendung);
$idsToRemove = $verwendungCollection->pluck('id')->toArray();
$this->partsClient->reject(function ($value, $key) use ($item) {
return in_array($value->id, $idsToRemove);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/381827.html
上一篇:ExtJS7.3.0GMapClasssic工具包標記組件
下一篇:Laravel聯系表無法正常作業
