All individual subscriptions are 50% off this week only!

Gabotronix
1 month ago
293
2
General

Array to string conversion error when running laravel seeders

Posted 1 month ago by Gabotronix

I'm getting the error below when running seeders for my appointments table:

  ErrorException  : Array to string conversion

  at C:\xampp\htdocs\daisyspet\vendor\laravel\framework\src\Illuminate\Support\Str.php:353
    349|
    350|         $result = array_shift($segments);
    351|
    352|         foreach ($segments as $segment) {
  > 353|             $result .= (array_shift($replace) ?? $search).$segment;
    354|         }
    355|
    356|         return $result;
    357|     }

  Exception trace:

  1   Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Array to string conversion", "C:\xampp\htdocs\daisyspet\vendor\laravel\framework\src\Illuminate\Support\Str.php")
      C:\xampp\htdocs\daisyspet\vendor\laravel\framework\src\Illuminate\Support\Str.php:353

  2   Illuminate\Support\Str::replaceArray("?", "insert into `appointments` (`petservice_id`, `user_id`, `fullName`, `petInfo`, `petNumber`, `comments`, `days`, `interval`, `phone`) values (?, ?, ?, ?, ?, ?, ?, ?, ?)")
      C:\xampp\htdocs\daisyspet\vendor\laravel\framework\src\Illuminate\Database\QueryException.php:56

  Please use the argument -v to see more details.

Appointments seede file looks like this:

<?php


use Illuminate\Database\Seeder;


class AppointmentsSeeder extends Seeder
{
   

    public function run()
    {
        
        DB::table('appointments')->insert([
            'petservice_id' => 3,
            'user_id' => 2,
            'fullName' => 'John Doe',
            'petInfo' => 'My dog is very good',
            'petNumber' => 1,
            'comments' => 'I have no special requests',
            'days' => [ 'jueves', 'viernes' ],
            'interval' => ['5-10-2019', '15-11-2019'],
            'phone' => 6566332122
        ]);
    }
}

And migration:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAppointmentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('appointments', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('user_id')->index();
            $table->foreign('user_id')->references('id')->on('users');
            $table->unsignedInteger('petservice_id')->index();
            $table->foreign('petservice_id')->references('id')->on('petservices');
            $table->string('fullName');
            $table->string('phone');
            $table->longText('petInfo');
            $table->integer('petNumber');
            $table->text('interval');
            $table->text('days');
            $table->longText('comments')->nullable();
            $table->timestamps();
        });
    }

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

And just in case my model, notice how I use casts property to transform jso into array, maybe that's why I'm getting this error:

<?php
namespace App\Models;


use Illuminate\Database\Eloquent\Model;


class Appointment extends Model
{
    
    
    protected $table = 'appointments';
    protected $casts = [ 'days' => 'array', 'interval' => 'array'];

    public function petservice()
    {
        //antes tenia hasOne
        return $this->belongsTo('App\Models\PetService', 'petservice_id');
    }

    public function user()
    {
        return $this->belongsTo('App\Models\User', 'user_id');
    }
    
    
}

Please sign in or create an account to participate in this conversation.