Я хочу связать свои заказы с продуктами которые входят в них, но получаю данную ошибку.
Мой контроллер метод:
public function orderAdd(OrderRequest $request)
{
$cart = Session::get('cart', []);
$orderData = $request->all();
foreach ($cart as $productId => $product) {
$orderData = $request->all();
$orderData['user_id'] = auth()->user()->id;
$orderData['color'] = $product['color'];
$orderData['size'] = $product['size'];
$orderData['price'] = $product['price'];
$newOrder = Order::create($orderData);
$newOrder->products()->attach($productId);
}
}
Связь в модели Product:
public function orders()
{
$this->belongsToMany(Order::class, 'orderProduct', 'product_id', 'order_id')->withPivot('quantity');
}
Связь в модели Order:
public function products()
{
$this->belongsToMany(Product::class, 'orderProduct', 'order_id', 'product_id')->withPivot('quantity');
}
Миграция таблицы для связи:
public function up(): void
{
Schema::create('orderProduct', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('product_id');
$table->unsignedBigInteger('order_id');
$table->integer('quantity');
$table->foreign('order_id', 'order_fk')->references('id')->on('orders');
$table->foreign('product_id', 'productfororder_fk')->references('id')->on('products');
});
}