get item relations

This commit is contained in:
Kavalar 2024-11-27 17:11:06 +03:00
parent bba35c7b14
commit 2133fae2cc
5 changed files with 58 additions and 16 deletions

View File

@ -3,7 +3,9 @@
namespace app\modules\tag;
use Illuminate\Database\Eloquent\Model;
use itguild\forms\builders\SelectBuilder;
use itguild\forms\builders\TextInputBuilder;
use itguild\forms\inputs\Select;
use kernel\app_modules\tag\models\Tag;
use kernel\app_modules\tag\models\TagEntity;
use kernel\helpers\Debug;
@ -19,6 +21,7 @@ class TagModule extends Module
public MenuService $menuService;
public MigrationService $migrationService;
public function __construct()
{
$this->menuService = new MenuService();
@ -58,10 +61,12 @@ class TagModule extends Module
public function formInputs(string $entity, Model $model = null): void
{
$input = TextInputBuilder::build("tag", [
$input = SelectBuilder::build("tag[]", [
'class' => 'form-control',
'placeholder' => 'Теги',
'value' => Tag::find($model->id)->label ?? ""
'value' => '',
'multiple' => "multiple",
'options' => Tag::getTagLabelByEntity($entity)
]);
$input->setLabel("Теги");
$input->create()->render();
@ -69,23 +74,21 @@ class TagModule extends Module
public function saveInputs(string $entity, Model $model, Request $request): void
{
$tag = new Tag();
$tag->label = $request->post('tag');
$tag->entity = $entity;
$tag->status = Tag::ACTIVE_STATUS;
$tag->slug = Slug::createSlug($request->post('tag'), $model);
$tag->save();
TagEntity::where("entity", $entity)->where("entity_id", $model->id)->delete();
$tags = $request->post("tag");
foreach ($tags as $tag) {
$tagEntity = new TagEntity();
$tagEntity->entity = $entity;
$tagEntity->entity_id = $model->id;
$tagEntity->tag_id = $tag->id;
$tagEntity->tag_id = $tag;
$tagEntity->save();
}
}
public function getInputs(string $entity, Model $model)
public function getItems(string $entity, Model $model): array
{
// Debug::dd($tag);
return TagEntity::where("entity", $entity)->where("entity_id", $model->id)->with("tag")->get()->toArray();
}
}

View File

@ -185,4 +185,19 @@ class EntityRelation
}
}
}
public function getEntityAdditionalProperty(string $entity, Model $model): array
{
$relations = $this->getEntityRelationsBySlug($entity);
if ($relations){
foreach ($relations as $relation){
$moduleClass = $this->getAdditionalPropertyClassBySlug($relation);
if ($moduleClass and method_exists($moduleClass, "getItems")) {
return $moduleClass->getItems($entity, $model);
}
}
}
return [];
}
}

View File

@ -42,4 +42,20 @@ class Tag extends Model
];
}
public static function getTagListByEntity(string $entity): array
{
return self::where("entity", $entity)->get()->toArray();
}
public static function getTagLabelByEntity(string $entity): array
{
$result = [];
$tags = self::getTagListByEntity($entity);
foreach ($tags as $tag){
$result[$tag['id']] = $tag['label'];
}
return $result;
}
}

View File

@ -24,4 +24,9 @@ class TagEntity extends Model
'entity_id' => 'Идентификатор сущности',
];
}
public function tag(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Tag::class);
}
}

View File

@ -25,6 +25,9 @@ $table->beforePrint(function () use ($content) {
return $btn;
});
$entityRelation = new \kernel\EntityRelation();
$additional = $entityRelation->getEntityAdditionalProperty("post", $content);
$table->rows([
'created_at' => function ($data) {
if (!$data){