|ProjectTaskTemplate whereLastUsedAt($value) * @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskTemplate whereUseCount($value) * @mixin \Eloquent */ class ProjectTaskTemplate extends AbstractModel { /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'project_id', 'name', 'title', 'content', 'sort', 'is_default', 'userid', 'use_count', 'last_used_at' ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'last_used_at' => 'datetime', ]; /** * 关联项目 * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function project() { return $this->belongsTo(Project::class); } /** * 关联创建者 * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function user() { return $this->belongsTo(User::class, 'userid'); } /** * 原子递增使用次数并刷新最近使用时间。 */ public function incrementUsage(): void { $this->newQuery() ->where('id', $this->id) ->update([ 'use_count' => \DB::raw('use_count + 1'), 'last_used_at' => now(), ]); } }