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