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/Repository/Schema/SchemaRepository.php
<?php


namespace App\Repository\Schema;


use App\Models\DevicesSchema;
use App\Models\Schema;
use App\Models\SchemaParts;
use App\Repository\BaseRepository;
use App\Traits\GridTrait;
use Illuminate\Database\Eloquent\Builder;

class SchemaRepository extends BaseRepository
{

    use GridTrait;

    /**
     * @return Builder
     */
    public static function getQuery(): Builder
    {
        return Schema::query();
    }

    public static function createDeviceSchemaRelation($device_id, $schema_id)
    {
        $ds = DevicesSchema::query()->where('device_id', $device_id)->where('schema_id', $schema_id)->first();
        if (!$ds) {
            $ds = new DevicesSchema();
            $ds->device_id = $device_id;
            $ds->schema_id = $schema_id;
            $ds->save();
        }
    }

    public static function save($data, $device_id): Schema
    {
        $schema = Schema::where('external_id', $data['external_id'])->withTrashed()->first();
        if (!$schema) {
            $schema = new Schema();
        } else {
            $schema->deleted_at = null;
        }
        $schema->fill($data);
        $schema->save();

        self::createDeviceSchemaRelation($device_id, $schema->id);

        return $schema;
    }

    public static function update($data, $schema, $device_id): Schema
    {
        $schema->deleted_at = null;
        $schema->fill($data);
        $schema->update();

        self::createDeviceSchemaRelation($device_id, $schema->id);

        return $schema;
    }

    public static function delete($external_id): int
    {
        SchemaParts::where('schema_external_id', $external_id)->delete();
        Schema::where('external_id', $external_id)->delete();

        return 0;
    }
}