Home > OS >  I am a Laravel beginner, can anyone tell me how I can insert an array into database?
I am a Laravel beginner, can anyone tell me how I can insert an array into database?

Time:09-28

These are the values I want to insert into the database:

  $name4 = $request['name4'];
  $email4 = $request['email4'];
  $phone = $request['phone'];
  $cat = $request['cat'];
  $rname = $request['rname'];
  $file = $request['file'];
  $ingr = $request['ingr'];
  $qty = $request['qty'];
  $unit = $request['unit'];
  $recp = $request['recp'];
  $items = array(
    'ingredients'  => $ingr,
    'quantity' => $qty,
    'unit'     => $unit
  );

And insert query is as follows:

DB::INSERT("insert into tbl_contest (name, email, phone, category, recp_name, file, ingredient, recp_desc)" . "values('$name4','$email4','$phone','$cat','$rname','$file','$items','$recp')");

I wanted to add $ingr, $qty and $unit into the column ingredient.

Can anyone help me?

Thanks in advance

CodePudding user response:

The $items variable can not be an array, you can turn it into a json string.

$items= json_encode(array(
    'ingredients'  => $ingr,
    'quantity' => $qty,
    'unit'     => $unit
  ));
  • Related