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

74 lines
1.5 KiB
PHP
Raw Normal View History

2021-04-20 11:16:34 +02:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
2021-04-20 11:16:34 +02:00
class Customers extends Model
{
/**
* @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;
}
/**
* Printed Walls Relation
*
* @return HasMany
*/
public function printedWalls()
{
return $this->hasMany(PrintedWalls::class, 'customer_id', 'id');
2021-04-20 19:51:30 +02:00
}
2021-04-20 11:16:34 +02:00
}