| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use Illuminate\Database\Eloquent\Relations\HasOne;
- use Illuminate\Database\Eloquent\SoftDeletes;
- /**
- * @property int $id
- * @property string $document
- * @property string|null $rg
- * @property int $user_id
- * @property float|null $average_rating
- * @property int $total_services
- * @property string|null $birth_date
- * @property bool $selfie_verified
- * @property bool $document_verified
- * @property bool $is_approved
- * @property float|null $daily_price_8h
- * @property float|null $daily_price_6h
- * @property float|null $daily_price_4h
- * @property float|null $daily_price_2h
- * @property int|null $profile_media_id
- * @property \Illuminate\Support\Carbon|null $created_at
- * @property \Illuminate\Support\Carbon|null $updated_at
- * @property \Illuminate\Support\Carbon|null $deleted_at
- * @property-read \App\Models\User $user
- * @property-read \App\Models\Media|null $profileMedia
- * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider newModelQuery()
- * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider newQuery()
- * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider onlyTrashed()
- * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider query()
- * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider withTrashed(bool $withTrashed = true)
- * @method static \Illuminate\Database\Eloquent\Builder<static>|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<string, string>
- */
- 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");
- }
- }
|