1

I got a simple problem today. I'm working in a Livewire Component, I have a collection of objects, when I click on an object I want to add it to an array (which works) and then want to remove it from the collection - so that object isn't clickable anymore. What I have:

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);
        }  }  }

but it is not working because I dont know the key of the element which contains the matching id.

$this->verwendung is my array which holds the id in $this->verwendung[0]['id'] and $this->partsClient is my collection which comes from the database.

How can I modify the collection without making it to an array and then check against matching ids and then recollect? Or better how can I retrieve the matching key of the collection, so that I can use forget() ? Thanks to all here!

1 Answer 1

2

Instead of checking if the id is contained in the collection, you can just filter it with reject() directly

foreach ($this->verwendung as $item) {
    $this->partsClient->reject(function ($value, $key) use ($item) {
        return $value->id == $item['id'];
    });
}

Or simply

$verwendungCollection = collect($this->verwendung);

$idsToRemove = $verwendungCollection->pluck('id')->toArray();

$this->partsClient->reject(function ($value, $key) use ($item) {
    return in_array($value->id, $idsToRemove);
});

Sign up to request clarification or add additional context in comments.

4 Comments

Hi N69S, I won't work :( - I have implemented both versions in my code but the collection still is filled with all Objects. So I tested this code: I filled up my array with values and created a collection and tried to filter the collection with reject() - colletion stays the same :(
` $this->verwendung[] = ['id' => 4, 'partBezeichnung' => 'bez']; $this->verwendung[] = ['id' => 55, 'partBezeichnung' => 'bez2']; $checkarr[] = ['id' => 12, 'partBezeichnung' => 'bez12']; $checkarr[] = ['id' => 13, 'partBezeichnung' => 'bez13']; $checkarr[] = ['id' => 4, 'partBezeichnung' => 'bez4']; $checkColl = collect($checkarr); foreach ($this->verwendung as $item) { $checkColl->reject(function ($value, $key) use ($item) { return $value['id'] === $item['id']; }); } dd($checkColl);`
What am I missing? sorry for the comment code I don't know how to correctly format it in comments
ok delete my comments please ... I've tested it in tinker and it worked as expected. My fault - I have an error in the component, I dont know where :) but I am a small step closer to the finish :) Thx - N69S

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.