自动不在 laravel 中插入 created_at 和 updated_at 无效
Automatically not insert created_at and updated_at in laravel not working
自动不在 laravel 中插入 created_at 和 updated_at 无效。
DB::table('commundityvendordata')->insert($commdata);
我正在使用上面的语句传递一个数组来插入多条记录 $commdata 工作正常但是当检查我的 created_at 和 updated_at 数据库中的列时没有得到时间戳。
型号:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class commundityvendordata extends Model
{
use HasFactory;
protected $table ='commundityvendordata';
}
迁移:
public function up()
{
Schema::create('commundityvendordata', function (Blueprint $table) {
$table->bigIncrements('vender_id')->unsignedBigInteger();
$table->date('new_date');
$table->unsignedBigInteger('cd_id');
$table->foreign('cd_id')->references('cd_id')->on('communitydata')->onDelete('cascade');
$table->unsignedBigInteger('cn_id');
$table->foreign('cn_id')->references('cd_id')->on('communitydata')->onDelete('cascade');
$table->unsignedBigInteger('unit_id');
$table->foreign('unit_id')->references('unit_id')->on('units')->onDelete('cascade');
$table->unsignedInteger('vender1');
$table->unsignedInteger('vender2');
$table->unsignedInteger('vender3');
$table->unsignedInteger('vender4');
$table->timestamps();
});
}
控制器:
function commundityvendordata(Request $request){
$collection = count(collect($request));
$cvendordata = new commundityvendordata;
for ($i=0; $i < $collection; $i++) {
$new_date = Carbon::parse($request->new_date)->format('Y-m-d');
$CCode=communitydata::where('c_code','=',$request->ccode[$i])->first();
$cd_id = $CCode['cd_id'];
$CName=communitydata::where('c_name','=',$request->cname[$i])->first();
$cn_id = $CName['cd_id'];
$CUnit=units::where('unit','=',$request->cunit[$i])->first();
$unit_id = $CUnit['unit_id'];
$vender1 = $request->vendor1[$i];
$vender2 = $request->vendor2[$i];
$vender3 = $request->vendor3[$i];
$vender4 = $request->vendor4[$i];
$commdata = [
'new_date' => $new_date,
'cd_id' => $cd_id,
'cn_id' => $cn_id,
'unit_id' => $unit_id,
'vender1' => $vender1,
'vender2' => $vender2,
'vender3' => $vender3,
'vender4' => $vender4
];
if($cd_id != ''){
DB::table('commundityvendordata')->insert($commdata);
}else{
return back()->with('success','Data Saved...');
}
}
}
数据库table 屏幕截图:
created_at
和 updated_at
仅在您使用 Eloquent
时自动插入。
如果您使用的是 DB
门面,则必须手动插入它们:
$commdata = [
'new_date' => $new_date,
'cd_id' => $cd_id,
'cn_id' => $cn_id,
'unit_id' => $unit_id,
'vender1' => $vender1,
'vender2' => $vender2,
'vender3' => $vender3,
'vender4' => $vender4,
"created_at" => \Carbon\Carbon::now(), # new \Datetime()
"updated_at" => \Carbon\Carbon::now(), # new \Datetime()
];
如果您想充分利用 Laravel 而没有 advanced/complex 查询,我建议您使用模型和 Eloquent。
这是因为您还没有设置 created_at
和 updated_at
的默认值。为此,您可以遵循 2-3 种解决方案:
解决方案 1
- 转到 phpmyadmin
- Select 数据库 table。 (在你的情况下,它是
commundityvendordata
)
- 然后转到结构并设置
created_at
和updated_at
的默认值
- 对于
updated_at
,您可以将属性设置为更新 Current_Timestamp
解决方案 2
在您的 commdata
数组中,只需添加 created_at
和 updated_at
$commdata = [
...
'created_at' => date("Y-m-d h:i:s"),
'updated_at' => date("Y-m-d h:i:s"),
];
解决方案 3
是对你的Scheme进行修改
Schema::create('commundityvendordata', function (Blueprint $table) {
$table->timestamps('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP'));
});
}
自动不在 laravel 中插入 created_at 和 updated_at 无效。
DB::table('commundityvendordata')->insert($commdata);
我正在使用上面的语句传递一个数组来插入多条记录 $commdata 工作正常但是当检查我的 created_at 和 updated_at 数据库中的列时没有得到时间戳。
型号:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class commundityvendordata extends Model
{
use HasFactory;
protected $table ='commundityvendordata';
}
迁移:
public function up()
{
Schema::create('commundityvendordata', function (Blueprint $table) {
$table->bigIncrements('vender_id')->unsignedBigInteger();
$table->date('new_date');
$table->unsignedBigInteger('cd_id');
$table->foreign('cd_id')->references('cd_id')->on('communitydata')->onDelete('cascade');
$table->unsignedBigInteger('cn_id');
$table->foreign('cn_id')->references('cd_id')->on('communitydata')->onDelete('cascade');
$table->unsignedBigInteger('unit_id');
$table->foreign('unit_id')->references('unit_id')->on('units')->onDelete('cascade');
$table->unsignedInteger('vender1');
$table->unsignedInteger('vender2');
$table->unsignedInteger('vender3');
$table->unsignedInteger('vender4');
$table->timestamps();
});
}
控制器:
function commundityvendordata(Request $request){
$collection = count(collect($request));
$cvendordata = new commundityvendordata;
for ($i=0; $i < $collection; $i++) {
$new_date = Carbon::parse($request->new_date)->format('Y-m-d');
$CCode=communitydata::where('c_code','=',$request->ccode[$i])->first();
$cd_id = $CCode['cd_id'];
$CName=communitydata::where('c_name','=',$request->cname[$i])->first();
$cn_id = $CName['cd_id'];
$CUnit=units::where('unit','=',$request->cunit[$i])->first();
$unit_id = $CUnit['unit_id'];
$vender1 = $request->vendor1[$i];
$vender2 = $request->vendor2[$i];
$vender3 = $request->vendor3[$i];
$vender4 = $request->vendor4[$i];
$commdata = [
'new_date' => $new_date,
'cd_id' => $cd_id,
'cn_id' => $cn_id,
'unit_id' => $unit_id,
'vender1' => $vender1,
'vender2' => $vender2,
'vender3' => $vender3,
'vender4' => $vender4
];
if($cd_id != ''){
DB::table('commundityvendordata')->insert($commdata);
}else{
return back()->with('success','Data Saved...');
}
}
}
数据库table 屏幕截图:
created_at
和 updated_at
仅在您使用 Eloquent
时自动插入。
如果您使用的是 DB
门面,则必须手动插入它们:
$commdata = [
'new_date' => $new_date,
'cd_id' => $cd_id,
'cn_id' => $cn_id,
'unit_id' => $unit_id,
'vender1' => $vender1,
'vender2' => $vender2,
'vender3' => $vender3,
'vender4' => $vender4,
"created_at" => \Carbon\Carbon::now(), # new \Datetime()
"updated_at" => \Carbon\Carbon::now(), # new \Datetime()
];
如果您想充分利用 Laravel 而没有 advanced/complex 查询,我建议您使用模型和 Eloquent。
这是因为您还没有设置 created_at
和 updated_at
的默认值。为此,您可以遵循 2-3 种解决方案:
解决方案 1
- 转到 phpmyadmin
- Select 数据库 table。 (在你的情况下,它是
commundityvendordata
) - 然后转到结构并设置
created_at
和updated_at
的默认值
- 对于
updated_at
,您可以将属性设置为更新Current_Timestamp
解决方案 2
在您的 commdata
数组中,只需添加 created_at
和 updated_at
$commdata = [
...
'created_at' => date("Y-m-d h:i:s"),
'updated_at' => date("Y-m-d h:i:s"),
];
解决方案 3
是对你的Scheme进行修改
Schema::create('commundityvendordata', function (Blueprint $table) {
$table->timestamps('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP'));
});
}