Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

markzimmer's avatar

Hiding duplicate foreign keys when doing a left join

I have 3 tables

1)question 2)category 3)moderator

The question table has foreign key references for both category and moderator id. What want to do is to show all the questions along with the moderators and category name as title(once) before all the questions belonging to that category.How do i hide the category and moderator except the first which will display before all questions for the category

e.g

category:food moderator:jack

Q1,Q2 ... Qn

category:animals moderator:james

Q1,Q2 ... Qn

Note:English is not my first language ask clarification.

0 likes
3 replies
nsvetozarevic's avatar

Your db is set up wrong. Better way would be to have reference to moderator in category table. Then you could do something like this in the view:

@foreach ($categories as $category)
    {{ $category->name  . '  ' . $category->moderator->name }}
    @foreach ($category->questions as $question)
        {{ $question->text }}
    @endforeach
@endforeach
markzimmer's avatar

Is this right??

category table cat_id|moderator_id|category_name

moderator mod_id|mod_name

question table q_id|mod_id|cat_id|question

nsvetozarevic's avatar

@markzimmer If I get it right you have one moderator per category, right? Why would you need a reference to moderator table in questions then? And also you should try to follow laravel naming conventions. Something like this:

categories id|moderator_id|name

moderators id|name

questions id|category_id|question

Afterwards, you can take a look at Eloquent relations to see how can you set up relations properly, and then you can write the code I mentioned above

Please or to participate in this conversation.