Get an inverse relationship with Laravel

7

I am creating a forum system that is divided by sections. Each section contains categories, each category contains topics. Ex:

Administração _
               |_Regras da do fórum
               |_Sugestões e críticas

Aeromodelismo _
               |_Elétricos _
               |_Glow       |-Tópico 1
                            |-Tópico 2
                            |-Tópico N

When I enter the forum I need to pick up the last topics answered where where ) the section id = X.

In summary I need to get the last topics answered within a specific section, but it should be noted that in the 'topics' table the foreign key refers to the category, and in the 'categories' table the foreign key refers to the section, that is, the topics are 'grandchildren' of the sections.

    
asked by anonymous 17.02.2014 / 16:35

2 answers

4

Well, I would implement it this way:

Unify the Categories and sessions into a single table by adding a column "id_pai", referencing the table itself and allowing sessions to be null. So the sessions would be Categories with id_pai null.

To return the topics of a specific session, just DB::table('sessoes')->where('id_pai', 4)->get()->toArray(); to have an array with the id of all categories and finally to use this array in a query with whereIn

Example:

$categorias = DB::table('sessoes')->where('id_pai', 4)->get()->toArray();
$topicos = DB::table('topicos ')
                    ->whereIn('id',$categorias)->get();
    
25.02.2014 / 02:16
1

If the problem is just ordering ...

Model::orderBy('id', 'DESC')->get();

More information link

    
17.02.2014 / 16:41