gpt4 book ai didi

php - 错误: Object of class Illuminate\Database\Eloquent\Builder could not be converted to string

转载 作者:行者123 更新时间:2023-11-29 15:12:45 24 4
gpt4 key购买 nike

我不知道为什么我的代码不起作用。这是代码。

date_default_timezone_set('Asia/Jakarta');
$pk1 = Presensi::select('pukul1');
$pk2 = Presensi::select('pukul2');

$pk1 = Carbon::createFromFormat('H-i-s', $pk1);
$pk2 = Carbon::createFromFormat('H-i-s', $pk2);
$t_jam = $pk1->diffInHours($pk2);
$presensi->update(
[
'jam_kerja_2' => $request -> jk1,
'pukul2' => Carbon::now(),
'Total' => $t_jam
]
);
return redirect()->route('admin.presensi.index');

模型/迁移看起来像这样

Schema::create('presensis', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('karyawan_id')->nullable()->unsigned();
$table->String('nama');
$table->String('jam_kerja_1', 10)->nullable();
$table->Time('Pukul1')->nullable();
$table->String('jam_kerja_2', 10)->nullable();
$table->Time('Pukul2')->nullable();
$table->String('Total')->nullable();
$table->timestamps();
});

最佳答案

因为您的Pukul1Pukul2的类型是Time,所以它的格式是H:i:s ;

而且你没有抽出时间。您需要使用获取记录并获取其属性值。

因此您需要将代码更改为如下所示:

$pk1 = Presensi::select('pukul1')->first()->pukul1;
$pk2 = Presensi::select('pukul2')->first()->pukul2;

$pk1 = Carbon::createFromFormat('H:i:s', $pk1);
$t_jam = $pk1->diffInHours($pk2);

关于php - 错误: Object of class Illuminate\Database\Eloquent\Builder could not be converted to string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59943910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com