public function cas() {
if(! $this->exists) {
return 0;
}
$changed_but_no_save = $this->getDirty();
if(! $changed_but_no_save) {
return 0;
}
if(array_diff(array_keys($changed_but_no_save), array_keys($this->original))) {
return 0;
}
if($this->usesTimestamps()) {
$this->updateTimestamps();
$changed_but_no_save = $this->getDirty();
}
$update_field_name = $this->getUpdatedAtColumn();
$updated_at_val = $this->{$update_field_name};
$query = $this->query()->where($this->getKeyName(), $this->getKey())
->when($updated_at_val, function (Builder $query) use($update_field_name, $updated_at_val) {
$query->where($update_field_name, $updated_at_val);
});
foreach($changed_but_no_save as $k => $v) {
$query->where($k, $this->getOriginal($k));
}
$res = $query->update($changed_but_no_save);
if($res > 0) {
$this->syncChanges();
$this->syncOriginal();
}
return $res;
}
$res = ModelName::find($id);
$res->field_name = value;
dd($res->cas());
DB::listen(function ($query) {
Log::channel('db')->info($query->sql, $query->bindings);
});
'db' => [
'driver' => 'daily',
'path' => storage_path('logs/db.log'),
'level' => env('LOG_LEVEL', 'debug'),
'days' => 7,
],
本作品採用《CC 協議》,轉載必須註明作者和本文連結