Answer the question
In order to leave comments, you need to log in
How to add an object to a table?
Good evening. There is a table orders: id, user_id, product_id, count, price, date; Sending ajax object with nested arrays. We need to add all these arrays to the orders table. I did, but it throws an error. Yes, I doubt I'm doing it right. Tell me how to implement it correctly?
public function makeorder()
{
$data = Request::all();
$date = new date('d.m.Y');
foreach ($data as $item) {
Order::create(['user_id' => Auth::user()->id], ['product_id' => $item[0]], ['count' => $item[1], ['price' => $item[2]], ['date'] => $date]);
}
}
Answer the question
In order to leave comments, you need to log in
Decided)
public function makeorder()
{
$data = Request::all();
$date = date('d.m.Y');
foreach ($data as $item) {
DB::table('orders')->insert([
'user_id' => Auth::user()->id,
'product_id' => $item[0],
'count' => $item[1],
'total' => $item[2],
'date' => $date,
]);
}
}
I need to change this code:
On that:
Order::create([
'user_id' => Auth::user()->id,
'product_id' => $item[0],
'count' => $item[1],
'price' => $item[2],
'date' => $date,
]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question