97 lines
2.5 KiB
PHP
97 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
class Customer extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'email'
|
|
];
|
|
|
|
/**
|
|
* CustomerInfo hasMany relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function infos()
|
|
{
|
|
return $this->hasMany(CustomerInfo::class);
|
|
}
|
|
|
|
/**
|
|
* Get the CustomerInfo from a specific brand
|
|
*
|
|
* @param Brand $brand
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
*/
|
|
public function info(Brand $brand)
|
|
{
|
|
return CustomerInfo::whereHas('brand', function (Builder $query) use ($brand) {
|
|
$query->where('id', $brand->id);
|
|
})->whereHas('customer', function (Builder $query) use ($brand) {
|
|
$query->where('id', $this->id);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* CustomerLogin hasMany relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function logins()
|
|
{
|
|
return $this->hasMany(CustomerLogin::class);
|
|
}
|
|
|
|
/**
|
|
* Get the CustomerLogin from a specific brand
|
|
*
|
|
* @param Brand $brand
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
*/
|
|
public function login(Brand $brand)
|
|
{
|
|
return CustomerLogin::whereHas('brand', function (Builder $query) use ($brand) {
|
|
$query->where('id', $brand->id);
|
|
})->whereHas('customer', function (Builder $query) use ($brand) {
|
|
$query->where('id', $this->id);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* CustomerOptInHistory hasMany relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function optInHistory()
|
|
{
|
|
return $this->hasMany(CustomerOptInHistory::class);
|
|
}
|
|
|
|
/**
|
|
* Customer Brand belongsToMany relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\belongsToMany
|
|
*/
|
|
public function brands()
|
|
{
|
|
return $this->belongsToMany(Brand::class, CustomerInfo::class);
|
|
}
|
|
|
|
public function setOptIn(Brand $brand, bool $optIn)
|
|
{
|
|
// Ajout dans la table d'historique des opt-in de la nouvelle valeur
|
|
$optInHistory = new CustomerOptInHistory();
|
|
$optInHistory->customer_id = $this->id;
|
|
$optInHistory->brand_id = $brand->id;
|
|
$optInHistory->new_value = $optIn;
|
|
$optInHistory->saveOrFail();
|
|
}
|
|
}
|