73 lines
1.5 KiB
PHP
73 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Customers extends Model
|
|
{
|
|
/**
|
|
* @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) {
|
|
$customer->country = (new Customers())->getIsoCountry($customer->country);
|
|
});
|
|
|
|
static::updating(function ($customer) {
|
|
$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)));
|
|
switch ($country) {
|
|
case 'Belgique':
|
|
$country = 56;
|
|
break;
|
|
case 'Luxembourg':
|
|
$country = 442;
|
|
break;
|
|
default:
|
|
$country = 250;
|
|
}
|
|
return $country;
|
|
}
|
|
|
|
/**
|
|
* Printed Walls Relation
|
|
*
|
|
* @return HasMany
|
|
*/
|
|
public function printedWalls()
|
|
{
|
|
return $this->hasMany(PrintedWalls::class, 'customer_id', 'id');
|
|
}
|
|
}
|