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

padamghimire75@gmail.com's avatar

How to insert data to new table?

I have 3 tables

  1. users
  2. products (user_id,product_name)
  3. total_products (product_name,type) *type=[old,new]
  public function products(){
        return $this->hasMany(Product::class,'user_id','id');
    }

Now i want to insert old products fetch from user and products relation and new data from request

0 likes
7 replies
lat4732's avatar

I think you need to reconstruct your question and explanations, because nothing is understood from them.

vainway 's avatar

@padamghimire75@gmail.com you are not explaining well what you want, are you getting errors, or you're suggesting something you didn't try. I provided some codes that you can try to insert into the database

public function products(Request $request)
    {
            $this->validate($request, [
                'product_name' => 'required|min:3',
            ]);
    
            $products= Product::create([
                'product_name' => $request->product_name,
                'user_id' => Auth::user()->id
            ]);
		return view('your-project-name', [
            'products' => $products,
        ]);
   }

you should have user_id as a foreign key and you can use the same code to insert data on total_products

padamghimire75@gmail.com's avatar

@niyo I just don't want to write same code twice. i want insert old and an new data by adding status to it i.e old,new

vainway 's avatar

@padamghimire75@gmail.com you want to add the status if the product is new or old then try the codes below

public function products(Request $request)
    {
            $this->validate($request, [
                'product_name' => 'required|min:3',
				'type' => 'required|string',
            ]);
    
            $products= Product::create([
                'product_name' => $request->product_name,
				'type' => $request->type,
                'user_id' => Auth::user()->id
            ]);
		return view('your-project-name', [
            'products' => $products,
        ]);
   }

in your blade.php

<form action="/product-add" method="POST">
                @csrf
                <div class="form-group">
                    <label>Product Name</label>
                    <input id="product_name" type="text" name="product_name" value="{{ old('product_name') }}" placeholder="Name..." class="form-control @error('product_name') is-invalid @enderror" required autocomplete="product_name">
                    @error('product_name')
                        <span class="invalid-feedback" role="alert">
                            <strong>{{ $message }}</strong>
                        </span>
                    @enderror
                </div>
                <div class="form-group">
                    <label>Product Status</label>
                    <select name="type" class="form-control">
                        <option value="new">new</option>
                        <option value="old">old</option>
                        <option value="">none</option>
                    </select>
                </div>
<button type="submit" class="btn btn-success">submit</button>
<form/>

in your route

 Route::POST('/product-add', 'Admin\ProductController@products');
vainway 's avatar

@padamghimire75@gmail.com no one can help you if don't show where the problem is or your code well so we can fix it for you, it is confusing for everyone to know what you want

Please or to participate in this conversation.