'integer', 'mileage' => 'integer', 'crawl_brand_id' => 'integer', 'crawl_series_id' => 'integer', 'crawl_msrp' => 'integer', 'crawl_car_id' => 'integer', 'status' => 'integer', 'sort' => 'integer', 'attachment' => 'array', 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; /** * 状态常量 */ const STATUS_NORMAL = 1; // 正常 const STATUS_ARCHIVED = 2; // 归档 const STATUS_DELETED = 3; // 删除 /** * 关联用户 */ public function user() { return $this->belongsTo(User::class, 'user_id'); } /** * 关联PDF生成日志 */ public function pdfLogs() { return $this->hasMany(UsedCarPdfLog::class, 'used_car_id')->orderBy('created_at', 'desc'); } /** * 判断是否可以编辑 */ public function canEdit(): bool { return $this->status === self::STATUS_NORMAL; } /** * 判断是否已删除 */ public function isDeleted(): bool { return $this->status === self::STATUS_DELETED; } /** * 判断是否已归档 */ public function isArchived(): bool { return $this->status === self::STATUS_ARCHIVED; } /** * 获取图片URL数组 */ public function getImageUrlsAttribute(): array { if (empty($this->images)) { return []; } return explode(',', $this->images); } /** * 作用域:正常状态 */ public function scopeNormal($query) { return $query->where('status', self::STATUS_NORMAL); } /** * 作用域:按用户筛选 */ public function scopeByUser($query, int $userId) { return $query->where('user_id', $userId); } /** * 作用域:搜索关键词 */ public function scopeSearch($query, string $keyword) { return $query->where(function ($q) use ($keyword) { $q->where('product_name', 'like', "%{$keyword}%") ->orWhere('code', 'like', "%{$keyword}%"); }); } }