我正在使用 Laravel 9 开发一个 Web。我使用命令 sail php make:migration add_bought_to_products_table 在产品表中添加一个名为“bought”的布尔列。当尝试使用 Eloquent 帮助程序修改值时 (Product::where('id', $product->id)->update(array('bought'=>true)) 数据库中的值未更新< /strong>.当查看它时,我发现迁移创建的新字段“购买”被标记为只读:没有相应的表列。
迁移代码如下:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('products', function (Blueprint $table) {
$table->boolean('bought');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('products', function (Blueprint $table) {
$table->dropColumn('bought');
});
}
};
这是数据库的屏幕截图:
我已经多次尝试清理缓存并重建数据库,并再次进行回滚和迁移。奇怪的是,我之前添加了“可见性”字段,它与出现问题的字段使用完全相同的代码和步骤完美地工作。
如何解决?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我只是重新启动了我的数据库工具,它就消失了。
在我绞尽脑汁之后,我通过简单地重新启动 docker 容器来解决了这个问题。看来这个问题与 Laravel 无关,而是与 Docker 有关!
对于遇到类似问题的任何人:确保结束 Docker 和所有容器并重新启动。