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

Amalmax's avatar

how to put topic_id to the product table in Laravel

working with Laravel and I have both Topic and Product Tables. Product table contains relate to the Topic.

Topic.php

class Topic extends Model
{
    public function products()
      {
        return $this->hasMany('App\Product');
      }
}

Product.php

class Product extends Model
{
    public function Topic()
    {
        return $this->belongsTo('App\Topic');
    }
}

and I have the following controller store function in ProductController

public function store(Request $request)
    {
        $this->validate($request, array(
            'name'=> 'required|max:225',
            'menu'=> 'required|max:225',
            'slug' => 'required|max:225',
            'description'=> 'required|max:225',
        ));

        $product = new Product;

        $product->name = $request->name;
        $product->menu = $request->menu;
        $product->slug = $request->slug;
        $product->description = $request->description;
        $product->link = $request->link;
        $product->topic_id = $request->topic_id;
      
        $product->save();
    }

web.php

Route::resource('product','ProductController');

and Product/create.blade.php

<div class="card-body">
                  <form method="post" action="{{action('ProductController@store')}}" enctype="multipart/form-data">
                  {{csrf_field()}}
                    <div class="form-group">
                    <label for="exampleFormControlInput1">Product Name</label>
                    <input type="text" name="name" id="name" class="form-control" placeholder="Product Name">
                    </div>

                    <div class="form-group">
                    <label for="exampleFormControlInput1">Menu</label>
                    <input type="text" name="menu" id="menu" class="form-control" placeholder="Menu">
                    </div>

                    <div class="form-group">
                    <label for="exampleFormControlInput1">Slug</label>
                    <input type="text" name="slug" id="slug" class="form-control" placeholder="Slug">
                    </div>

                    <div class="form-group">
                    <label for="exampleFormControlInput1">Body</label>
                    <input type="text" name="description" id="description" class="form-control" placeholder="Body">
                    </div>

                    

                    <div class="form-group">
                    <label for="exampleFormControlInput1">Upload Product Image</label>
                    <input type="file" name="image" id="image" class="form-control" placeholder="Product Image">
                    </div>

                    <div class="form-group">
                    <label for="exampleFormControlInput1">Link</label>
                    <input type="text" name="link" id="link" class="form-control" placeholder="Link">
                    </div>
 <button type="submit" class="btn btn-success btn-lg btn-block" style="margin-top:10px">Create New Product</button>
                  </form>

Then I need to store topic_id in the Product Table. how could I pass topic_id to the ProductController store function?

0 likes
15 replies
tykus's avatar

You have no topic_id input in the form; how does the user decide which Topic to associate with the new Product?

Your options to pass this data into the Controller are either using the URL (segment or query parameter), or in the POST body as form data; generally we use the latter.

shami003's avatar

If you want to select some topic from your database, fetch all the topics in your create page, show it in select options, pass that topic_id to store method

UsmanBasharmal's avatar

$request->topic_id; means that you are posting the id from the form but in your form, there isn't any field or any dropdown box that you should get the id and post it to the controller. second post your tables that we can see the columns and let us know what do you actually want to do

Amalmax's avatar

@tykus @shami003 @usmanbasharmal Please see my route url and here is my topic id when some user needs to enter product data relating to the topic. so, I need inter this topic id to the Product table

tykus's avatar

There is no topic_id in the URL

Amalmax's avatar

@tykus please see this route here

Route::get('/create_product/topic/{topic}', function () {
    return view('products.create');
 })->name('products.create');
tykus's avatar
tykus
Best Answer
Level 104

This is new information. In any case, this is the GET route which displays the form; it does not follow that the POST request URI will have the same topic segment.

You can grab the topic segment from the URI, and make a hidden input on the form:

<input type="hidden" name="topic_id" value="{{ request()->route()->parameter('topic') }}">

Now, your store method will have the topic_id Request data to set on the Product instance.

Amalmax's avatar

@tykus one thing then how could I save topic id? is this correct way

$product->topic()->associate($topic);
tykus's avatar

Your original $product->topic_id = $request->topic_id; is just fine.

Amalmax's avatar

got following error message here ErrorException Undefined variable: topic_id

tykus's avatar

Did you put the hidden input on the form like I described earlier? And are you accessing that value on the Request using:

$request->topic_id
Amalmax's avatar

yes of course see my codes again, web.php

Route::get('/create_product/topic/{topic}', function () {
    return view('products.create');
 })->name('products.create');

form

<input type="hidden" name="topic_id" value="{{ request()->route()->parameter('topic') }}">

and controller store function

$product->topic_id = $request->$topic_id;
tykus's avatar

Seriously? I gave you this $request->topic_id and you did this $request->$topic_id - do you see the difference?

Amalmax's avatar

yep, my mistaken now it is working fine Thanks

UsmanBasharmal's avatar

it is right because you are not passing any topic _id to it. give a topick_id in URL which is in your database and then try what you will get

Please or to participate in this conversation.