QR_code_generator/app/Models/Objects/FieldsGroup.php

55 lines
1.2 KiB
PHP

<?php
namespace App\Models\Objects;
use App\Support\UuidScopeTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
class FieldsGroup extends Model {
use UuidScopeTrait, SoftDeletes;
protected $dates = [
];
protected $fillable = [
'uuid',
'object_type_id',
'name',
'title',
'hidden',
'params'
];
protected $hidden = [
'id'
];
protected $casts = [
'params' => 'array'
];
public function fields(): BelongsToMany {
return $this->belongsToMany(Field::class, 'fields_group_fields', 'group_id')->orderByPivot('ord');
}
public function objectType(): BelongsTo {
return $this->belongsTo(ObjectType::class);
}
public function syncFields(array $fieldsNames) {
$data = [];
collect($fieldsNames)->each(function($name, $i) use(&$data) {
if ($field = Field::query()->where('name', $name)->first()) $data[$field->id] = ['ord' => $i];
});
$this->fields()->sync($data);
}
}