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

yijani's avatar

Select category for product form

Hi guys, I have a Category with id, name and other columns.

I'm creating a form to register a new product and I want the user to select the category he wants the product in.

I was wondering, what is the best way to create a select for the user with the category?

0 likes
6 replies
ankitparmar372's avatar

example : 1.category table id , name 2.product table id,category_id,name than define relationship one(category) to many (product) in laravel and get data as you want.

yijani's avatar

I know the relationship part, I'm talking about passing the data to the product registration forma where you can select the category you want the product in using a select tag.

Christianus's avatar

@yijani You can store it first then passing it

$data = array( 'id' => $request->get('id'), 'name' => $request->get('name') );

then passing it return view('user.yourformblade')->with($data); << just example

then if you want to get it just use {{$id}} or {{$name}} on your form

yijani's avatar

I don't think you guys understood my question.

It's like this, I have a controller called ProductController and on the create method:

    public function create()
    {
        $categories = Category::get()->pluck('name', 'id');

        return view('site.create', compact('categories'));
    }

Now I want to put the categories in a select html tag but as I'm using pluck I don't have the $category->name and $category->id.

cviv's avatar
cviv
Best Answer
Level 3
$categories = Category::select('name', 'id')->get();
2 likes

Please or to participate in this conversation.