Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

php - How to insert values for SET Column type for Laravel

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
      IlluminateDatabaseQueryBuilder::insert()

      +32 vendor frames 
  35  artisan:37
      IlluminateFoundationConsoleKernel::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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For SET column, your insert method should be :

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...