75 lines
1.6 KiB
PHP
75 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Country;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Kwaadpepper\Enum\Traits\CastsEnums;
|
|
|
|
class CustomerInfo extends Model
|
|
{
|
|
use HasFactory;
|
|
use CastsEnums;
|
|
|
|
protected $enumCasts = [
|
|
'country' => Country::class
|
|
];
|
|
|
|
protected $fillable = [
|
|
'firstname',
|
|
'lastname',
|
|
'birth_date',
|
|
'address',
|
|
'address2',
|
|
'phone',
|
|
'country',
|
|
'city',
|
|
'postcode',
|
|
'opt_in'
|
|
];
|
|
|
|
protected static function booted()
|
|
{
|
|
static::creating(function ($customerInfo) {
|
|
$customerInfo->hashcode = Str::uuid();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Customer BelongsTo relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function customer()
|
|
{
|
|
return $this->belongsTo(Customer::class);
|
|
}
|
|
|
|
/**
|
|
* Brand BelongsTo relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function brand()
|
|
{
|
|
return $this->belongsTo(Brand::class);
|
|
}
|
|
|
|
/**
|
|
* Get customer info from email
|
|
*
|
|
* @param 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);
|
|
});
|
|
}
|
|
}
|