*/ protected $fillable = [ 'firstname', 'lastname', 'birth_date', 'address', 'address2', 'phone', 'country', 'city', 'postcode', 'opt_in' ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'country' => Country::class, 'opt_in' => 'bool', ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::creating(function (self $model) { $model->hashcode = Str::uuid(); }); static::deleting(function (self $model) { CustomerOptInHistory::query()->whereHas('customer', function (Builder $q) use ($model) { $q->where('id', $model->customer->id); })->whereHas('brand', function (Builder $q) use ($model) { $q->where('id', $model->brand->id); })->delete(); }); } // * RELATIONS /** * Customer belongs to relation. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function customer(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Customer::class); } /** * Brand belongs to relation. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function brand(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Brand::class); } // * METHODS /** * Get Login. * * @return \App\Models\CustomerLogin */ public function login(): \App\Models\CustomerLogin { return $this->customer->login($this->brand); } /** * Get customer info from email. * * @param \App\Models\Brand $brand * @param string $email * @return \Illuminate\Database\Eloquent\Builder */ public static function getFromEmail(Brand $brand, string $email): \Illuminate\Database\Eloquent\Builder { return CustomerInfo::query()->whereHas('brand', function (Builder $q) use ($brand) { $q->where('id', $brand->id); })->whereHas('customer', function (Builder $q) use ($email) { $q->where('email', $email); }); } /** * Get customer info from hashcode. * * @param \App\Models\Brand $brand * @param string $hashcode * @return \Illuminate\Database\Eloquent\Builder */ public static function getFromHashcode(Brand $brand, string $hashcode): \Illuminate\Database\Eloquent\Builder { return CustomerInfo::query()->whereHas('brand', function (Builder $q) use ($brand) { $q->where('id', $brand->id); })->where('hashcode', $hashcode); } }