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

luarsab's avatar

Array to string conversion in Laravel

Hello, i got an error named array to string conversion.

how i got it ? ---> i have created custom request php artisan make:request somenameRequest, then i have a form, from where im getting this error.. im just simply writing

    modelname::create([
   'something' => $request->something
  ]);

and etc.. im writing everything correctly, i got that this dont work only for ONE model, for other models this works, what can be an issue?

im trying to create student for user (sub user), so when im creating this student im getting an array to string conversion, but when im trying to create user by instead of Student, it works fine, i have

  protected $guarded = ['id'] 

in student model, and my students migration is connected by constrained only to user_id (without cascade on delete)

0 likes
4 replies
Sinnbeck's avatar

You are getting an array but expect a string. Cannot say more without the actual code

1 like
luarsab's avatar

function in controller

        public function submitStudent(CreateStudentRequest $request): RedirectResponse
{

   $user = auth()->user();

   Student::create([
       'first_name' => $request->first_name,
       'last_name' => $request->last_name,
       'balances' => []
   ]);

      return redirect()->route('parent.create-student_submit', ['user_id' => auth()->user()->id]);
   }

custom request

 class CreateStudentRequest extends FormRequest
  {  
          public function authorize()
      {
         return true;
      }


public function rules()
{
    return [
        'last_name'      => 'required',
        'first_name'     => 'required',
        'country'        => 'required',
        'street_address' => 'required',
        'city'           => 'required',
        'state'          => 'required',
        'zip'            => 'required',
    ];
}

}

ROUTE

   Route::post('/parent/create-student/{user_id}', [ParentController::class, 'submitStudent'])->name('parent.create-student_submit');

VIEW

  <form id="form" method="POST" action="{{route('parent.create-student_submit', ['user_id' => $user_id])}}" class="mt-8 space-y-6 w-full">
        @csrf
        <div class="bg-white p-8">
            <div>
                <h3 class="text-lg font-medium leading-6 text-gray-900">Personal Information</h3>
                <p class="mt-1 text-sm text-gray-500">Use a permanent address where you can receive mail.</p>
            </div>
            <div class="mt-6 grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-6">
                <div class="sm:col-span-2">
                    <label for="last-name" class="block text-sm font-medium text-gray-700">Last name</label>
                    <div class="mt-1">
                        <input type="text" value="{{old('last_name')}}" required name="last_name" id="last_name" autocomplete="given-name" class="block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm">
                    </div>
                </div>

                <div class="sm:col-span-2">
                    <label for="first-name" class="block text-sm font-medium text-gray-700">First name</label>
                    <div class="mt-1">
                        <input type="text" value="{{old('first_name')}}" required name="first_name" id="first_name" autocomplete="family-name" class="block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm">
                    </div>
                </div>
                <div class="sm:col-span-2">
                    <label for="middle-name" class="block text-sm font-medium text-gray-700">Middle name</label>
                    <div class="mt-1">
                        <input type="text" value="{{old('middle_name')}}" name="middle_name" id="middle_name" autocomplete="additional-name" class="block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm">
                    </div>
                </div>

(i did not copied all view, because i dont think something iis in view, just to showcase)

! MIGRATION (user)!

     return new class() extends Migration {
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
	Schema::create('users', function (Blueprint $table) {
		$table->id();
		$table->string('first_name')->default('unfilled');
		$table->string('last_name')->default('unfilled');
		$table->string('middle_name')->nullable();
		$table->string('email')->unique();
		$table->string('password');
		$table->foreignId('school_id')->default(1)->constrained();
		$table->integer('billingo_id')->default(0);
		$table->tinyInteger('summary_frequency')->default(0);
		$table->tinyInteger('finished_onboarding')->default(0);
		$table->json('user_information')->nullable();
		$table->string('two_factor_token')->nullable()->unique();
		$table->boolean('is_verified')->default(false);
		$table->rememberToken();
		$table->timestamps();
	});
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
	Schema::dropIfExists('users');
}

};

!! MIGRATION !! (student)

     return new class extends Migration
     {
      public function up()
           {
              Schema::create('students', function (Blueprint $table) {
            $table->id();
            $table->string('first_name');
           $table->string('last_name');
            $table->string('middle_name')->nullable();
           $table->string('card_number')->nullable();
          $table->string('card_data')->nullable();
           $table->json('user_information')->nullable();
          $table->json('balances');
          $table->foreignId('user_id')->constrained();
          $table->foreignId('school_id')->constrained('users','school_id');
          $table->timestamps();
      });
  }

           public function down()
        {
       Schema::dropIfExists('students');
      }
       };

User model :

protected $guarded = ['id'];

protected $hidden = [
	'password',
	'remember_token',
];

public function school(): BelongsTo
{
	return $this->belongsTo(School::class);
}

public function students(): HasMany
{
    return $this->hasMany(Student::class);
}

Student model :

class Student extends Model

{ use HasFactory;

 protected $guarded = ['id'];

public function user(): BelongsTo
{
    return $this->belongsTo(User::class);
}

public function school(): BelongsTo
{
    return $this->belongsTo(School::class);
}

public function transactions(): HasMany
{
    return $this->hasMany(Transaction::class);
}

}

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You are passing an array to balances

 'balances' => []

Is it supposed to be json? If so, you can add this to the student model to have laravel cast it.

protected $casts = [
    'balances' => 'array'
];
2 likes
luarsab's avatar

@Sinnbeck Omg thats it.... i just wanted to send null there for a while, and then wanted to change the field to nullable in migration after i finish (to win time), but with my choice i just loosed 2 hours :D thank you

Please or to participate in this conversation.