I made a self-learning blog, in which posts and tags are interconnected through an intermediate table (pivot table), respectively, there are three tables:
Schema::create('posts', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name', 80); $table->text('description')->nullable(); $table->boolean('show')->default(0); $table->timestamps(); }); Schema::create('tags', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name', 80); }); Schema::create('posts_to_tags', function (Blueprint $table) { $table->integer('post_id'); $table->integer('posts_to_tags_id'); $table->string('posts_to_tags_type'); });
There is some page on which tags are located. According to the logic of things, it is necessary to display only those tags that already have posts, but not to display those tags that have empty posts.
For this solution, I found several solutions to the problem.
First:
- make a selection from the tags table and check the availability of posts
Of the minuses worth noting:
- You will have to make a full selection of tags and sort them completely, which would entail an extra load.
The second option:
- It consists in sampling the intermediate table and the subsequent sampling of values from it, it looks like something wild, I would even say, but I could not finish this option because I had to access the intermediate table directly, and not as usual through the table models.
Of the minuses:
- it will be necessary to make several requests in order to get all the used tags from the intermediate table, and there will obviously be more of them than the entire tags tag.
Are there any normal options for solving the current problem, or will you have to choose the first option and not *** gasp your brain?
Damn, as if holding the treasured duck in my hands, while telling all this I realized that the first version looks the most promising and easier. What do you think?