|Provider newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|Provider newQuery() * @method static \Illuminate\Database\Eloquent\Builder|Provider onlyTrashed() * @method static \Illuminate\Database\Eloquent\Builder|Provider query() * @method static \Illuminate\Database\Eloquent\Builder|Provider withTrashed(bool $withTrashed = true) * @method static \Illuminate\Database\Eloquent\Builder|Provider withoutTrashed() * @mixin \Eloquent */ class Provider extends Model { use HasFactory, SoftDeletes; protected $table = "providers"; protected $guarded = ["id"]; /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ "birth_date" => "date", "selfie_verified" => "boolean", "document_verified" => "boolean", "is_approved" => "boolean", "average_rating" => "decimal:1", "daily_price_8h" => "decimal:2", "daily_price_6h" => "decimal:2", "daily_price_4h" => "decimal:2", "daily_price_2h" => "decimal:2", "total_services" => "integer", ]; } /** * @return BelongsTo */ public function user(): BelongsTo { return $this->belongsTo(User::class, "user_id"); } /** * @return BelongsTo */ public function profileMedia(): BelongsTo { return $this->belongsTo(Media::class, "profile_media_id"); } /** * @return HasMany */ public function addresses(): HasMany { return $this->hasMany(Address::class, 'source_id') ->where('source', 'provider'); } // /** // * @return HasOne // */ // public function primaryAddress(): HasOne // { // return $this->hasOne(Address::class, 'source_id') // ->where('source', 'provider') // ->where('is_primary', true); // } /** * @return HasMany */ public function blockedClients() { return $this->hasMany(ProviderClientBlock::class); } /** * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function blockedByClients() { return $this->hasMany(ClientProviderBlock::class); } public function updateAverageRating(float $newRating): void { $totalReviews = Review::where('reviews.origin', 'client') ->leftJoin('schedules', 'schedules.id', '=', 'reviews.schedule_id') ->where('schedules.provider_id', $this->id) ->count(); if ($totalReviews === 0) { $this->average_rating = $newRating; } else { $currentTotalRating = $this->average_rating * ($totalReviews - 1); $newAverage = ($currentTotalRating + $newRating) / $totalReviews; $this->average_rating = round($newAverage, 2); } $this->save(); } public function primaryAddress() { return $this->hasOne(Address::class, "source_id") ->where("source", "provider") ->orderBy("is_primary", "desc"); } }