G
G
GaserV2016-10-10 19:20:26
Laravel
GaserV, 2016-10-10 19:20:26

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

2 answer(s)
G
GaserV, 2016-10-10
@GaserV

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,
      ]);
    }
    
  }

M
Mokhirjon Naimov, 2016-10-10
@zvermafia

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 question

Ask a Question

731 491 924 answers to any question