ldc-mesbr-app/app/Models/Customers.php

96 lines
2 KiB
PHP
Raw Permalink Normal View History

2021-04-20 11:16:34 +02:00
<?php
namespace App\Models;
use App\Models\Panier;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2021-04-20 11:16:34 +02:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
2021-04-20 11:16:34 +02:00
class Customers extends Model
{
use HasFactory;
/**
* @var string $primaryKey
*/
2021-08-13 11:48:06 +02:00
protected $primaryKey = 'hashcode';
/**
* @var string $keyType
*/
2021-08-20 11:46:17 +02:00
protected $keyType = 'string';
2021-04-20 19:51:30 +02:00
/**
* @var array $fillable
*/
2021-08-13 11:48:06 +02:00
protected $fillable = ['hashcode', 'email', 'country'];
2021-04-20 19:51:30 +02:00
/**
* booted method
*
* @return void
*/
2021-04-20 19:51:30 +02:00
protected static function booted()
{
static::creating(function ($customer) {
2021-04-27 17:29:47 +02:00
$customer->country = (new Customers())->getIsoCountry($customer->country);
2021-04-20 19:51:30 +02:00
});
static::updating(function ($customer) {
2021-04-27 17:29:47 +02:00
$customer->country = (new Customers())->getIsoCountry($customer->country);
2021-04-20 19:51:30 +02:00
});
}
/**
* getIsoCountry method
* Default is : France
*
* @param string $country
* @return integer
*/
2021-04-27 17:29:47 +02:00
private function getIsoCountry($country)
2021-04-20 19:51:30 +02:00
{
2021-04-27 17:29:47 +02:00
$country = trim(ucfirst(strtolower($country)));
switch ($country) {
2021-04-20 19:51:30 +02:00
case 'Belgique':
$country = 56;
break;
2021-04-20 19:51:30 +02:00
case 'Luxembourg':
$country = 442;
break;
2021-04-20 19:51:30 +02:00
default:
$country = 250;
2021-04-20 19:51:30 +02:00
}
return $country;
}
/**
2025-08-14 17:05:09 +02:00
* Printed Coupons Relation
*
* @return HasMany
*/
2025-08-14 17:05:09 +02:00
public function printedCoupons()
{
2025-08-14 17:05:09 +02:00
return $this->hasMany(PrintedCoupons::class, 'customer_id', 'id');
2021-04-20 19:51:30 +02:00
}
/**
* Basket model relation
* @return HasMany
*/
public function baskets()
{
return $this->hasMany(Basket::class, 'customer_id', 'id');
}
2025-08-14 17:05:09 +02:00
/**
2025-10-29 12:41:39 +01:00
* Panier model relation
2025-08-14 17:05:09 +02:00
* @return HasMany
*/
public function paniers()
{
2025-10-29 12:41:39 +01:00
return $this->hasMany(Panier::class, 'panier_contactId', 'id');
2025-08-14 17:05:09 +02:00
}
2021-04-20 11:16:34 +02:00
}