LearnProgramming
LearnProgramming

Reputation: 806

Laravel, how to eliminate some of the "pluck" value for dropdown selection?

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 value from the "furnitures" table, how do I eliminate the value that contain from "furniture_prices" table in the pluck queries result? So in this case, user can select "fur5", "fur6", "fur8", "fur9", "fur10", "fur11" and fur12"

Upvotes: 4

Views: 229

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

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

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

If the relationship doesn't exist yet, create hasOne() or hasMany() relationship first:

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

Upvotes: 2

Related Questions