Resolution of "Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, int given, called in" in Laravel
Thank you for your continued support.
This article contains advertisements that help fund our operations.
Table Of Contents
⇨ Click here for the table of contents of Laravel articles
This article summarizes the solution for the error "Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, int given, called in" in Laravel.
Conclusion: "You need to append to the model if you specify json for the database column"
Add the following to the model
protected $casts = [
'column_name' => 'json',
];
Detailed explanation
Migration file
In Laravel migration files, you can save arrays in columns.
There may be various cases where you might use it when it's difficult to create tables in many-to-many relationships.
At that time, you write the following in the migration file.
$table->json('post_ids'); //post_ids is the column name. post_id is assumed to be an array.
When saving in controllers etc.
$model = new Model;
$model->post_ids = [1,2,3,4,5];
$model->save();
You can save like this, but when saving, an error occurred:
Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, int given, called in
Append to the model
For example, if you were to add it to the User model, it would look like this.
<?php
namespace App\Models\User;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected $casts = [
'post_ids' => 'json',
];
}
After adding this and trying again, the same error did not occur, and the data was inserted as expected. Yay!
Using it, you can use it as an array in a normal way
$ids = $user->post_ids // You can retrieve [1,2,3,4,5] with this,
foreach($ids as $id){
}
You can handle it as an array like this.
The point to note is that it is difficult to write relations
It is not suitable for displaying data from related tables, so be careful when using it!!
Summary
That's all.
I hope it is helpful to someone.
Please contact me via Twitter DM for feedback or complaints.
That's it!
Popular articles
Deploying PHP 7.4 + Laravel 6 Project to AWS EC2
Implementing Breadcrumbs in Laravel with laravel-breadcrumbs