英文:
Laravel new Model missing created_at and updated_at fields
问题
在控制器的更新函数中,当我使用 new Model
创建新模型时,缺少了 created_at
和 updated_at
字段。因此,我会收到 409 错误,因为它无法执行查询。有没有任何原因导致这两个字段缺失?
$branchItem = Branch::where('id', $branch['branchId'])->get()->first();
// 如果没有与 branchId 匹配的记录,则创建新记录
if (!$branchItem) {
$branchItem = new Branch;
}
$branchItem->id = $branch['branchId'];
$branchItem->save();
英文:
When I create a new Model in controller update function with new Model
it's missing created_at
and updated_at
. I therefore get error 409, as it is unable to perform the query. Any idea why those two fields are missing?
$branchItem = Branch::where('id', $branch['branchId'])->get()->first();
// if there is no record with branchId create new one
if (!$branchItem) {
$branchItem = new Branch;
}
$branchItem->id= $branch['branchId'];
$branchItem->save();
答案1
得分: 2
你还可以查看Laravel迁移文档
并且你还可以在你的迁移文件中按照下面的代码来操作:
Schema::create('table_name', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps(); // 这个字段会在数据库中创建created_at和updated_at字段。
});
所以你需要将上面的代码添加到你的迁移文件中,放在这里:project_folder/database/migrations/migration_file
一旦你添加了这行代码,你需要在终端中运行迁移命令:
php artisan migrate:refresh
注意:当你运行这个命令时,它会清空所有表的数据并重置结构。
希望这能帮助你解决问题。
英文:
You can also check with the documentation of Laravel Migration
And you can also follow below code in your migration file
Schema::create('table_name', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps(); // this field is able to create your created_at and updated_at field in DB.
});
So you have to add above code in your migration file placed here project_folder/database/migrations/migration_file
Once you add this line you have to run migration command in terminal
php artisan migrate:refresh
Note: When you run this command, this will flush your all table's data and reset structure
Hope this will lead you to resolve problem.
答案2
得分: 1
创建created_at
和updated_at
列是手动操作,虽然不是最佳实践,但可以解决你的问题。
始终使用Laravel迁移来创建表格。
在这里,$table->timestamps();
负责创建这两个字段。
英文:
create created_at
and updated_at
column manually it's not best practice but it will solve your issue
always use laravel migration to create tabel
Schema::create('example', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
});
here $table->timestamps();
is responsible to create those 2 fields
答案3
得分: 0
缺少表中的created_at和updated_at,请将其视为时间戳,updated_at设置为NULL。
英文:
You are missing created_at and updated_at in table take it as a timestamp and updated_at as NULL
答案4
得分: 0
尝试更改代码如下:
$branchItem = Branch::where('id', $branch['branchId'])->first();
// 如果没有与branchId匹配的记录,则创建新记录
if (!$branchItem) {
$branchItem = new Branch;
$branchItem->id = $branch['branchId'];
$branchItem->save();
} else {
echo "记录已存在";
}
英文:
Try changing code like this.
$branchItem = Branch::where('id', $branch['branchId'])->first();
// if there is no record with branchId create new one
if (!$branchItem) {
$branchItem = new Branch;
$branchItem->id= $branch['branchId'];
$branchItem->save();
} else {
echo "Record Exist";
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论