In laravel 10 / livewire 3 app I have a listing of news with related tags shows and debugging sql I see a lot of similar requests like :
SELECT *
FROM `tags`
WHERE `tags`.`id` in (41, 42)
...
SELECT *
FROM `tags`
WHERE `tags`.`id` in (40, 41, 42)
So if listing of news has 20 rows I see 20 such requests. I have component :
class ModelTag extends Component
{
use AppCommonTrait;
public int $id;
public string $dataType;
public $modelTaggables = [];
public function render(): View
{
return view('livewire.model-tag');
}
public function mount(int $id, string $dataType)
{
$this->id = $id;
$this->dataType = $dataType;
$this->readDbData();
}
protected function readDbData(): void
{
$modelType = $this->getModelType($this->dataType);
$this->modelTaggables = Taggable
::getByTaggableType($modelType)
->getByTaggableId($this->id)
->with('tag')
->get()
->map(function ($TaggableItem) use($modelType) {
return ['id'=> $TaggableItem->tag_id, 'name'=> $TaggableItem->tag->name];
});
}
}
I would like to make performance of my app better and reduce number of similar sql-requests.
How can I do it in this case ?