dimanche 23 juin 2019

Array to string conversion error in migration

In my laravel 5.8 I set json field :

Schema::create('vote_categories', function (Blueprint $table) { $table->increments('id');

$table->string('meta_description', 255)->nullable();
$table->json('meta_keywords')->nullable();


$table->timestamp('created_at')->useCurrent();

and some init data in seeder :

DB::table( 'vote_categories' )->insert([
    'id'                 => 1,
    'name'               => 'Classic literature',
    'slug'               => 'classic-literature',
    'active'             => true,
    'in_subscriptions'   => true,
    'meta_description'   => '',
    'meta_keywords'      => ['Classic literature'],
]);

and in model :

class VoteCategory extends MyAppModel
{

    protected $table      = 'vote_categories';
    protected $primaryKey = 'id';
    public $timestamps    = false;

    protected $casts = [
        'meta_keywords' => 'array'
    ];

But running migration I got error :

$ php artisan migrate
Migration table created successfully.
...
Migrating: 2018_07_13_051201_create_vote_categories_table

   ErrorException  : Array to string conversion

  at /mnt/_work_sdb8/wwwroot/lar/votes/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", "/mnt/_work_sdb8/wwwroot/lar/votes/vendor/laravel/framework/src/Illuminate/Support/Str.php")
      /mnt/_work_sdb8/wwwroot/lar/votes/vendor/laravel/framework/src/Illuminate/Support/Str.php:353

  2   Illuminate\Support\Str::replaceArray("?", [], "insert into `vt2_vote_categories` (`id`, `name`, `slug`, `active`, `in_subscriptions`, `meta_description`, `meta_keywords`) values (?, ?, ?, ?, ?, ?, ?)")
      /mnt/_work_sdb8/wwwroot/lar/votes/vendor/laravel/framework/src/Illuminate/Database/QueryException.php:56

  Please use the argument -v to see more details.

Why error ? I supposed that $casts array must be used in ->insert methods, but it looks not like so.

How to fix it ?

Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire