ldc-mesbr-app/app/Models/Customers.php
2026-10-28 13:23:49 +01:00

93 lines
1.9 KiB
PHP

<?php
namespace App\Models;
use App\Models\Panier;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Customers extends Model
{
use HasFactory;
/**
* @var string $primaryKey
*/
protected $primaryKey = 'hashcode';
/**
* @var string $keyType
*/
protected $keyType = 'string';
/**
* @var array $fillable
*/
protected $fillable = ['hashcode', 'email', 'country'];
/**
* booted method
*
* @return void
*/
protected static function booted()
{
static::creating(function ($customer): void {
$customer->country = new Customers()->getIsoCountry($customer->country);
});
static::updating(function ($customer): void {
$customer->country = new Customers()->getIsoCountry($customer->country);
});
}
/**
* getIsoCountry method
* Default is : France
*
* @param string $country
* @return integer
*/
private function getIsoCountry($country)
{
$country = trim(ucfirst(strtolower($country)));
$country = match ($country) {
'Belgique' => 56,
'Luxembourg' => 442,
default => 250,
};
return $country;
}
/**
* Printed Coupons Relation
*
* @return HasMany
*/
public function printedCoupons()
{
return $this->hasMany(PrintedCoupons::class, 'customer_id', 'id');
}
/**
* Basket model relation
* @return HasMany
*/
public function baskets()
{
return $this->hasMany(Basket::class, 'customer_id', 'id');
}
/**
* Panier model relation
* @return HasMany
*/
public function paniers()
{
return $this->hasMany(Panier::class, 'panier_contactId', 'id');
}
}