Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cotations88's avatar

Illuminate\Database\Grammar::parameterize(): Argument #1 ($values) must be of type array, string given, called in C:\xampp\htdocs\world_konnect\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 1022

Hello everyone; l am looking for a query to insert and update the same row in INTEREST_Database where HOBBY column can store multiple checkbox values. this is my controller code

 public function updateInterest(Request $request)
       {
            $user_id = Auth::user()->id;
       //DB::table('interests')->where('user_id',$user_id)->update($request,except('token'));
      // DB::table('interests')->insert($request->toArray());
         
         $interest = new Interest();
          $interest->hobby = $request->hobby;
          //$input = $request->all();
        //$input['hobby'] = $request->input('hobby');
        $interest->language_1 = $request->language_1;
        $interest->language_2 = $request->language_2;
        $interest->language_3 = $request->language_3;
        $interest->language_4 = $request->language_4;
        $interest->language_5 = $request->language_5;
        $interest->marital_status = $request->marital_status;
        $interest->children = $request->children != null?"1":"0";

        $interest->user_id = Auth::id();
        $interest->save();

My model 

```<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Interest extends Model
{
    use HasFactory;

      protected $fillable = [
        'hobby',
        'language_1',
        'language_2',
        'language_3',
        'language_4',
        'language_5',
        'marital_status',
        'children',
        'user_id'
    ];
     public function user()
    {
        return $this->belongsTo('App\Models\User');
    }

      public function profile()
    {
     return $this->hasOne(Profile::class);
    }
}

my database
public function up(): void
{
    Schema::create('interests', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('user_id');
    $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
    $table->string('hobby[]')->nullable(); 
    $table->string('language_1')->nullable(); 
    $table->string('language_2')->nullable(); 
    $table->string('language_3')->nullable(); 
    $table->string('language_4')->nullable(); 
    $table->string('language_5')->nullable(); 
    $table->enum('marital_status',['Single','Married','Widowed','Separated','Divorced'])->default('Single');
   $table->boolean('children',['1','0'])->default('1');
   $table->timestamps();
    });
Thanks for your help
0 likes
3 replies
LaryAI's avatar
Level 58

The error message "Argument #1 ($values) must be of type array, string given" suggests that the $request variable passed to the updateInterest method is not an array. To fix this, you can convert the $request object to an array using the toArray() method before passing it to the update() method. Here's the updated code:

public function updateInterest(Request $request)
{
    $user_id = Auth::user()->id;
    $interest = Interest::where('user_id', $user_id)->first();
    if (!$interest) {
        $interest = new Interest();
        $interest->user_id = $user_id;
    }
    $interest->hobby = $request->input('hobby');
    $interest->language_1 = $request->input('language_1');
    $interest->language_2 = $request->input('language_2');
    $interest->language_3 = $request->input('language_3');
    $interest->language_4 = $request->input('language_4');
    $interest->language_5 = $request->input('language_5');
    $interest->marital_status = $request->input('marital_status');
    $interest->children = $request->input('children') ? 1 : 0;
    $interest->save();
}

In this updated code, we first check if an Interest record already exists for the current user. If it does, we update that record; otherwise, we create a new Interest record. We also use the input() method to retrieve the values from the $request object, and we convert the children value to a boolean using a ternary operator.

Snapey's avatar

You cannot store an array in mysql.

$table->string('hobby[]')->nullable();

will create a field called 'hobby[]' but it will still be a string type.

You could save it as json string or implode the values

$interest->hobby = implode(',',$request->hobby);
cotations88's avatar

@Snapey Thanks maetro , it works. How to display only updated row in view blade ? particularly hobby column with multiples values ?

Please or to participate in this conversation.