How to insert values for SET Column type for Laravel

Viewed 1151

I am trying to use the Laravel Set Column type. I have been able to create a table with it, but I couldn't find any information regarding how to actually insert data into it. My schema format

    Schema::create('bundles', function (Blueprint $table) {
        $table->id('BID');
        $table->set('bundle', ['A', 'B', 'C']);
    });

My code to try to insert

$insert = DB::table('bundles')->insert(['bundle'=>['A', 'B']]);

but it throws the error

   ErrorException 

  ksort() expects parameter 1 to be array, string given

  at vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2817
    2813▕         // in the same order for the record. We need to make sure this is the case
    2814▕         // so there are not any errors or problems when inserting these records.
    2815▕         else {
    2816▕             foreach ($values as $key => $value) {
  ➜ 2817▕                 ksort($value);
    2818▕ 
    2819▕                 $values[$key] = $value;
    2820▕             }
    2821▕         }

      +1 vendor frames 
  2   database/migrations/2020_12_24_072449_bundles.php:31
      Illuminate\Database\Query\Builder::insert()

      +32 vendor frames 
  35  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

I couldn't find any information on how to do this and the answer What is the MySQL datatype SET equivalent in Laravel Schema? doesn't explain how to insert.

1 Answers

For SET column, your insert method should be :

$insert = DB::table('bundles')
       ->insert(['bundle' => 'A,B']);
Related