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

xamshah's avatar

I want to put my customer ID in my order table

Hi guys! I have a table for Customers, Orders and OrderProducts. Now I want to put 'customer_id' into 'orders' table and also total items, 'quantity' and ' date'. This is how I save my customer data into customers table.

0 likes
8 replies
tykus's avatar

Code?

Aside, I would consider not putting the aggregated values in the orders tables as these can easily be derived from the related order_product table

xamshah's avatar

@tykus

class Customer extends Model { use HasFactory; protected $table='customers'; protected $fillable = [ 'firstname', 'lastname', 'email', 'phone', 'country', 'city', 'zipcode', 'address',

];
public function orders(){
    return $this->hasMany('App\Order');
}

} //this is for order model class Order extends Model { use HasFactory; protected $table='orders'; protected $fillable = [ 'customer_id', 'items', 'total', 'date', ];

public function customer(){
    // return $this->belongsTo(Customer::class,'customer_id','id');
    return $this->belongsTo('App\Customer');
} 

} //here is the method for customer table public function create (Request $request){ //for customer $this->validate($request,[ 'firstname' => ['required', 'string', 'max:255'], 'lastname' => ['required', 'string'], 'email' => ['required', 'string', 'email', 'max:255'], 'phone'=>['required'], 'country'=>['required'], 'city'=>['required'], 'zipcode'=>['required'], 'address'=>['required'], ]); $customer=new Customer; $customer->firstname = $request->input('firstname'); $customer->lastname = $request->input('lastname'); $customer->email = $request->input('email'); $customer->phone = $request->input('phone'); $customer->country = $request->input('country'); $customer->city = $request->input('city'); $customer->zipcode = $request->input('zipcode'); $customer->address = $request->input('address'); $save= $customer->save(); if($save){ session()->forget('cart'); return view('frontend.checkout.orderconfirm')->with('success','Order Placed'); } }

xamshah's avatar

@tykus

// customer model
class Customer extends Model
{
    use HasFactory;
    protected $table='customers';
    protected $fillable = [
        'firstname',
        'lastname',
        'email',
        'phone',
        'country',
        'city',
        'zipcode',
        'address',

    ];
    public function orders(){
        return $this->hasMany('App\Order');
    }
}
//Order Model
class Order extends Model
{
    use HasFactory;
    protected $table='orders';
    protected $fillable = [
        'customer_id',
        'items',
        'total',
        'date',
    ];

    public function customer(){
        // return $this->belongsTo(Customer::class,'customer_id','id');
        return $this->belongsTo('App\Customer');
    }
}
//here is the checkout controller
 public function create (Request $request){
                //for customer
        $this->validate($request,[
            'firstname' => ['required', 'string', 'max:255'],
            'lastname' => ['required', 'string'],
            'email' => ['required', 'string', 'email', 'max:255'],
            'phone'=>['required'],
            'country'=>['required'],
            'city'=>['required'],
            'zipcode'=>['required'],
            'address'=>['required'],
         ]);
         $customer=new Customer;
         $customer->firstname = $request->input('firstname');
         $customer->lastname = $request->input('lastname');
         $customer->email = $request->input('email');
         $customer->phone = $request->input('phone');
         $customer->country = $request->input('country');
         $customer->city = $request->input('city');
         $customer->zipcode = $request->input('zipcode');
         $customer->address = $request->input('address');
         $save= $customer->save();
        if($save){
            session()->forget('cart');
            return view('frontend.checkout.orderconfirm')->with('success','Order Placed');
        }
    }
//also migrations of both
 public function up()
    {
        Schema::create('customers', function (Blueprint $table) {
            $table->id();
            $table->string('firstname');
            $table->string('lastname');
            $table->string('email');
            $table->string('phone');
            $table->string('country');
            $table->string('city');
            $table->integer('zipcode');
            $table->string('address');
            $table->timestamps();
        });
    }
 public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('customer_id');
            $table->integer('items')->nullable();
            $table->integer('total')->nullable();
            $table->date('date')->nullable();
            $table->timestamps();
            $table->foreign('customer_id')->references('id')->on('customers')->onDelete('cascade');
        });
    }
tykus's avatar
tykus
Best Answer
Level 104

@xamshah you need to create the customer first and then the Orderand the oprde

 public function create (Request $request){
        $validatedData = $this->validate($request,[
            'firstname' => ['required', 'string', 'max:255'],
            'lastname' => ['required', 'string'],
            'email' => ['required', 'string', 'email', 'max:255'],
            'phone'=>['required'],
            'country'=>['required'],
            'city'=>['required'],
            'zipcode'=>['required'],
            'address'=>['required'],
         ]);

         \DB::transaction(function () use ($validatedData) {
             $customer= Customer::create($validatedData);
             $order = $customer->orders()->create([
                 'items' => 0, // get from the Cart
                 'total' => 0, // get from the Cart
                 'date' => now(),
             ]);
        });

        if($order){
            session()->forget('cart');
            return view('frontend.checkout.orderconfirm')->with('success','Order Placed');
        }
    }

I don't know how you store items in the Cart, or how you move them from the Cart into the order_products table; perhaps you can share that schema.

martinbean's avatar

Hi guys! I have a table for Customers, Orders and OrderProducts. Now I want to put 'customer_id' into 'orders' table and also total items, 'quantity' and ' date'. This is how I save my customer data into customers table.

@xamshah Cool. Do that then?

1 like

Please or to participate in this conversation.