mimiloca
1 month ago

How to make query for fetching specific id in eloquent many to many?

Posted 1 month ago by mimiloca

I'm trying to access a specific club id and displaying all the members under the same club id. This function is for admin page.

Below is the code that I have tried but it fetching all the user including the non members. How can I fix this query?


    club                member                     club_member 
id |     name   |     |id  |  name |        | id| id_club | id_member|
---|------------|     |----| ------|        |-------------------------
1  | Sc Club    |     | 1  | Maria |        | 1 |    1    |    1     |
2  | Mimes Club |     | 2  | Julia |        | 2 |    1    |    3     |
                      | 3  | Danny |        | 3 |    2    |    1     |
                      | 4  | Olie  |        | 4 |    2    |    2     |
                                            | 5 |    1    |    4     |



I'm trying to access the club id_2 and displaying the user under club id=2 which is Maria and Julia

public function show()
    {
        $clubs = Club::with('members')->get();
        return view('club.member')->with('club', $clubs); 
    }

Please sign in or create an account to participate in this conversation.