Laravel, how to eliminate the “plucking” part of the value for picking down?

controller

$getFurniture = Furniture::where('active', 1)->pluck('fur_name', 'id');
dump($getFurniture);

enter image description here

Table

enter image description here

As you can see, the codes return all the values ​​from the furniture table, how can I exclude the value that is contained in the furniture_prices table as a result of a pluck query? Therefore, in this case, the user can select "fur5", "fur6", "fur8", "fur9", "fur10", "fur11" and fur12 "

+4
source share
1 answer

I assume that you have already defined the relationship. Use the method doesntHave():

Furniture::where('active', 1)->doesntHave('furniturePrices')->pluck('fur_name', 'id');

If the relationship does not already exist, first create the relationship hasOne()or hasMany():

public function furniturePrices()
{
    return $this->hasMany(FurniturePrice::class, 'fur_id');
}
+2
source

Source: https://habr.com/ru/post/1692495/


All Articles