osb/app/Models/SupplierDetail.php

65 lines
1.1 KiB
PHP
Raw Normal View History

2021-12-20 03:25:43 +00:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
2021-12-20 03:25:43 +00:00
use App\Models\Supplier\{Broadband,Generic,Phone,Type};
2021-12-20 03:25:43 +00:00
use App\Traits\SiteID;
class SupplierDetail extends Model
{
use SiteID;
protected $casts = [ 'connections'=>'collection' ];
2021-12-20 03:25:43 +00:00
/* RELATIONS */
public function broadbands()
{
return $this->hasMany(Broadband::class);
}
public function generics()
{
return $this->hasMany(Generic::class);
}
public function phones()
{
return $this->hasMany(Phone::class);
}
2021-12-20 03:25:43 +00:00
public function supplier()
{
return $this->belongsTo(Supplier::class);
}
/* METHODS */
/**
* Find a supplier product of a particular type
*
* @param string $type
* @param int $id
* @return Type
* @throws \Exception
*/
public function find(string $type,int $id): Type
{
switch ($type) {
case 'broadband':
$item = $this->broadbands->where('id',$id);
break;
default:
throw new \Exception('Unknown type: '.$type);
}
if ($item->count() !== 1)
throw new ModelNotFoundException(sprintf('Unknown Model of type [%s] with id [%d]',$type,$id));
return $item->pop();
}
2021-12-20 03:25:43 +00:00
}