'boolean', 'inuavs' => 'array' ]; /** * The attributes that should be cast to Enums. * * @var array */ protected $enumCasts = [ 'presentation_type' => PresentationType::class, 'ocbStatus' => EleveurOcbStatus::class, 'ocbType' => OCBType::class, 'geo_type' => GeoType::class ]; /** * The table associated with the model. * * @var string */ protected $table = 'ocb_breeders'; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'breeder_code', 'first_name', 'last_name', 'postal_code', 'place', 'testimony', 'latitude', 'longitude', 'geo_type', 'ocbStatus', 'ocbType', 'presentation_type', 'audit', 'valid_marketing_date', 'video_link', 'status', 'display_random', 'inuavs', ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted() { static::creating(function ($breeder) { $slug = Str::slug(sprintf( '%s %s %s', $breeder->breeder_code, $breeder->last_name, $breeder->first_name )); if ($breeder->slug !== $slug and static::where('slug', $slug)->count()) { $slug .= sprintf('_%d', rand(100, 9000)); } $breeder->slug = $slug; OCBWaitingList::whereIn('inuav_code', $breeder->inuavs)->delete(); }); static::updating(function ($breeder) { $slug = Str::slug(sprintf( '%s %s %s', $breeder->breeder_code, $breeder->last_name, $breeder->first_name )); if ($breeder->slug !== $slug and static::where('slug', $slug)->count()) { $slug .= sprintf('_%d', rand(100, 9000)); } $breeder->slug = $slug; OCBWaitingList::whereIn('inuav_code', $breeder->inuavs)->delete(); }); static::deleted(function ($breeder) { $removePicture = function ($picture) { if ($picture) { if (strpos($picture, '/uploads') === 0) { File::delete(public_path($picture)); } else { Storage::disk('public')->delete($picture); } } }; $removePicture($breeder->getRawOriginal('picture')); $removePicture($breeder->getRawOriginal('picture1')); $removePicture($breeder->getRawOriginal('picture2')); $removePicture($breeder->getRawOriginal('picture3')); }); } /** * Get the route key for the model. * * @return string */ public function getRouteKeyName() { return "slug"; } // * METHODS /** * Get ocb validated breeders query * * @return \Illuminate\Database\Eloquent\Builder */ public static function ocbValidated() { return static::where('ocbStatus', EleveurOcbStatus::yes()); } /** * Getter picture attribute * * @return mixed */ protected function getPictureAttribute() { return $this->pictureAttribute('picture'); } /** * Getter picture_1 attribute * * @return mixed */ protected function getPicture1Attribute() { return $this->pictureAttribute('picture1'); } /** * Getter picture_2 attribute * * @return mixed */ protected function getPicture2Attribute() { return $this->pictureAttribute('picture2'); } /** * Getter picture_3 attribute * * @return mixed */ protected function getPicture3Attribute() { return $this->pictureAttribute('picture3'); } /** * Refactored picture attribute * * @param string $prop * @return mixed */ private function pictureAttribute(string $prop) { $p = $this->attributes[$prop]; // For migrated urls from 2016. if ( is_null($p) or !strlen($p) or strpos($p, '/uploads') === 0 ) { return $p; } return Storage::url($p); } }