File: /var/www/ipsremont-demo/app/Models/Device.php
<?php
namespace App\Models;
use App\Services\Device\DeviceService;
use App\Traits\Displayed;
use App\Traits\My;
use App\Traits\Sortable;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* @property int $id
* @property string $external_id
* @property string $category_code
* @property string $name
* @property string $photo
* @property string $specification
* @property string $deleted_at
* @property string $created_at
* @property string $updated_at
*
* @property Collection $parts
* @property Collection $schema
*/
class Device extends BaseModel
{
use SoftDeletes, Sortable, Displayed, My;
protected $table = 'devices';
protected $imageGallery = null;
public static string $imagePath = '/demo-images/';
public static string $specificationPath = '/specifications/';
protected $fillable = [
'external_id',
'category_code',
'name',
'photo',
'specification',
];
protected static $labels = [
'external_id' => 'devices.external_id',
'category_code' => 'devices.category_code',
'name' => 'devices.name',
'photo' => 'devices.photo',
'specification' => 'devices.specification',
];
public static function columns()
{
return [
"fields" => [
[
"displayName" => 'grid.external_id',
"field" => "external_id",
"sort" => true,
"sortType" => 'asc',
'class' => 'w-100px',
],
[
"displayName" => 'grid.category_code',
"field" => "category_code",
"sort" => true,
"sortType" => 'asc',
'class' => 'text-left',
],
[
"displayName" => 'grid.name',
"field" => "name",
"sort" => true,
"sortType" => 'asc',
'class' => 'text-left',
],
[
"displayName" => 'grid.photo',
"field" => "photo",
"sort" => true,
"sortType" => 'asc',
'class' => 'text-left',
],
[
"displayName" => 'grid.specification',
"field" => "specification",
"sort" => true,
"sortType" => 'asc',
'class' => 'text-left',
],
[
"displayName" => '',
"field" => 'action',
"sort" => false,
'class' => 'w-100px',
],
],
"sortDefault" => [
[
"field" => 'name',
"sort" => 'asc',
],
[
"field" => 'external_id',
"sort" => 'asc',
],
],
];
}
/**
* @return Part $part
*/
public function getParts()
{
return $this->parts();
}
public function getManufacturer()
{
return DeviceService::getManufacturer($this);
}
public function getName()
{
return DeviceService::getName($this);
}
public function getSerialName()
{
return $this->external_id . ', ' . DeviceService::getName($this);
}
public function getImage()
{
$mainImage = '';
if ($this->photo) {
$mainImage = explode(';', $this->photo)[0] ?? '';
}
return ($mainImage) ? self::$imagePath . trim($mainImage) : '';
}
public function getImageGallery()
{
if (is_array($this->imageGallery)) {
return $this->imageGallery;
}
$images = [];
if ($this->photo) {
$images = explode(';', $this->photo);
if (is_array($images) && count($images) > 0) {
array_shift($images);
} else {
$images = [];
}
}
array_walk($images, function (&$value, $key) {
$value = self::$imagePath . trim($value);
});
$this->imageGallery = $images;
return $this->imageGallery;
}
public function getSpecification()
{
$pathToFile = storage_path() . "/app/public/specifications/$this->specification";
return (file_exists($pathToFile)) ? $this->specification : '';
}
public function getNumber()
{
return $this->external_id;
}
// MARK: - Relations
public function parts():BelongsToMany
{
return $this->belongsToMany(Part::class, 'device_parts');
}
public function schema(): BelongsToMany
{
return $this->belongsToMany(Schema::class, 'devices_schema');
}
}