0

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Fathima
  • 3
  • 2
  • 1
    As a Laravel beginner: take a look at Eloquent ORM in the Laravel docs. It will make life easier. – Gert B. Sep 28 '21 at 08:17
  • PDO (which Laravel uses) cannot do any conversions from arrays to formats that the target databases support, since each database might or might not support arrays and each database that supports them supports them in different ways. Generally speaking putting an array as a value in a relational database is a bad idea and relationships should be used instead. – apokryfos Sep 28 '21 at 08:25
  • 1
    You can add `$items` and insert into a `JSON` column which accepts json array objects. You can define a [JSON column](https://laravel.com/docs/8.x/migrations#column-method-json) in your migration table and define a [cast](https://laravel.com/docs/8.x/eloquent-mutators#array-and-json-casting) to stay away from manually encoding and decoding your array. – Tithira Sep 28 '21 at 08:40
  • How do you plan to store such data? Usually, you don't store array in a database (see for example https://stackoverflow.com/questions/17371639/how-to-store-arrays-in-mysql/17371788 about this topic) – Nico Haase Sep 28 '21 at 14:14
  • Also, the given `DB::INSERT` call is widely open for SQL injection. Please do not use it anywhere. If you already use Laravel, check out any beginner tutorial about this topic – Nico Haase Sep 28 '21 at 14:17

1 Answers1

0

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
  ));
Plantour
  • 96
  • 5