Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

iamDiscovery's avatar

Pulling filter options from a different table.

Hey all, I was wondering whether it's possible to pull in a record's columns to use in a filter's options? For example, I have a categories table and a products table and was wondering whether I could pull in the name and ID column from categories into the filter which will be used on products. This was, if someone was to add a new category it would also get added to the filter.

public function options(Request $request)
    {
        return [

            'Hair' => 2,
            'Beauty' => 4,
            'Nails' => 5,
            'Barbers' => 6,
            'Brands' => 7,
            'Vegan' => 8,
            'Electricals' => 166,
            'Combos' => 167,
            'Starting Kits' => 168

        ];
    }

Here's an example of how I'm doing it currently, by manually adding the categories name and its ID. Whereas I'd want something like a foreach and then use categories->name => categories->id, if possible.

0 likes
1 reply
kevinbui's avatar

You can do so in a number of ways:

return Category::pluck('id', 'name')
    ->toArray();

// Or
return Category::pluck('name', 'id')
    ->flip()
    ->toArray();

// Or
return Category::all()
    ->mapWithKeys(function ($category) {
        return [$category->name => $category->id];
    })
    ->toArray();

Please or to participate in this conversation.