Laravel迁移:如何将boolean字段displayed的默认值改为0
How to Update the Default Value of the
displayed Boolean Column in Laravel Your current code will throw an error because you’re trying to create a column named displayed that already exists in the render table. To modify an existing column’s default value, you need to use Laravel’s change() method instead of redefining the column from scratch.
First, ensure you have the doctrine/dbal package installed—Laravel relies on this to handle column modifications:
composer require doctrine/dbal
Next, update your migration’s up() method to alter the existing column:
public function up() { Schema::table('render', function (Blueprint $table) { $table->boolean('displayed')->default(0)->change(); }); }
To keep your migration reversible (a best practice), add a down() method that reverts the default value back to 1:
public function down() { Schema::table('render', function (Blueprint $table) { $table->boolean('displayed')->default(1)->change(); }); }
Finally, run the migration to apply the changes:
php artisan migrate
内容的提问来源于stack exchange,提问作者Tomeikun
相关产品推荐
相关产品推荐

