Reputation: 341
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
Upvotes: 0
Views: 1618
Reputation: 2243
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 @Александр Черножуков.
Upvotes: 2
Reputation: 462
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
];
Upvotes: 2