HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
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');
    }

}