SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`mystock`.`product_warehouse`, CONSTRAINT `product_warehouse_warehouse_id_foreign` FOREIGN KEY (`warehouse_id`) REFERENCES `warehouses` (`id`))  /// insert into
  `product_warehouse` (`cost`, `price`, `product_id`, `qty`, `warehouse_id`)
values
  (50, 200, 220, 100, 100),
  (50, 200, 220, 100, 200),
  (50, 200, 220, 100, 50)

foreach ($this->productWarehouse as $key => $warehouseId) {
            $this->product->warehouses()->attach($warehouseId, [
                'price' => $this->productWarehouse[$key]['price'],
                'cost'  => $this->productWarehouse[$key]['cost'],
                'qty'   => $this->productWarehouse[$key]['quantity'],
            ]);
        } //  public function warehouses()
    {
        return $this->belongsToMany(Warehouse::class)
        ->withPivot('price', 'qty','cost');
    }