SQLSTATE[HY000]: General error: 1364 Field ‘item_id’ doesn’t have a default value (SQL: insert into `order_service

  html, javascript, laravel, php

This is my first time I make a system with an order…
I am confusing how can I insert data to the pivot table order_services

Tables:

order table: (id, total)

services table: (id, name, price)

item table:(id, name)

order_service table: (order_id,service_id, item_id, quantity, amount)

Models:

Order.php

protected $guarded = [];
public function services()
{
    return $this->belongsToMany(Service::class)->withPivot('order_id','service_id','item_id','quantity','amount');
  (Service::class,'order_service','order_id','service_id','item_id','quantity','amount');
}

Service.php:

  protected $guarded = [];

public function orders()
{
    return $this->belongsToMany(Order::class)->withPivot('order_id','service_id','item_id','quantity','amount');

  (Order::class,'order_service','order_id','service_id','item_id','quantity','amount');
}

OrderService.php:

protected $guarded = [];

    protected $table = 'order_service';

OrderController.php

public function store(Request $request)
{
        $data = request()->validate([
            'total' => 'required',
           ]);

       $order= Order::create($data);
           if ($request->serviceOrders) {
            foreach ($request->serviceOrders as $service) {

                $order->services()->attach($service);

Output:
Error
Function name must be a string

Source: Ask PHP

LEAVE A COMMENT