0

I am trying to post array data to the database but I keep getting this error Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in /home/**/****/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php on line 886

$nor = array(
            'no_of_rounds' => $request->no_of_rounds,
       );
 $data= New Item();
$data->no_of_rounds = $nor;
$data->save();

Blade

 <select class="selectpicker" name="no_of_rounds[]" multiple data-live-search="true" width="100%" id="no_of_rounds">
  <option value="90">Round 1</option>
  <option value="100">Round 2</option>
  <option value="110">Round 3</option>
   <option value="120">Round 4</option>
   <option value="130">Round 5</option>
   </select>

And when I dump I get this error enter image description here

6
  • 1
    You can encode the array as a JSON object and save to a JSON column with json_encode($nor) Commented Jul 9, 2021 at 7:01
  • @Tithira, how can I save to JSON? Commented Jul 9, 2021 at 7:02
  • 1
    replace this $data->no_of_rounds = $nor; with $data->no_of_rounds = json_encode($nor); . your error is mostly due to trying to push the array into a a column accepting a string, you may want to change your column type in your migration to $table->json('data'); to get rid of it Commented Jul 9, 2021 at 7:06
  • @Tithira and what about retrieving the stored array to the blade since it is not decoded? Commented Jul 9, 2021 at 7:18
  • Use the PHP function serialize() to convert arrays to strings. These strings can easily be stored in MySQL database. Using unserialize() something like : serialize($nor) Commented Jul 9, 2021 at 8:48

2 Answers 2

2

This error is thrown because you are trying to insert an array straight into the database. It first has to be serialized. You could store it as JSON. See example below.

$nor = array(
    'no_of_rounds' => $request->no_of_rounds,
);

$item = Item::create([
    'no_of_rounds' => json_encode($nor),
]);

The casting process can be automated by Laravel as proposed in another comment by @Александр Черножуков.

Sign up to request clarification or add additional context in comments.

10 Comments

and what about retrieving the stored array to blade since it is not decoded?
You can simply use json_decode($item->nor) for that. The answer of @Александр Черножуков gives you a solution so you do not have to that manually! If you set the casts attribute on your Item model, it will cast the automatically from array to JSON and vice versa.
I am getting this error when retrieving array, Call to undefined function jsond_encode()
You should use decode when retrieving the array. The correct function name is: json_decode(), not jsond_encode() you have made a typo in your function. So to encode use: json_encode(). To decode, use: json_decode().
Thanks, one more last question, how do I format the output "{\"no_of_rounds\":\"Round 1\"}"". . This is how I have done it {!! json_encode($order->no_of_rounds) !!}
|
2

This best resolve This will automatically convert to json when saving and back when reading into an array or collection, depending on your needs. Insert this code in your Model

protected $casts = [
        'no_of_rounds' => 'collection',//For collection
        'no_of_rounds' => 'array'//For array
    ];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.