E
E
EVOSandru62020-11-24 16:23:59
Laravel
EVOSandru6, 2020-11-24 16:23:59

How to convert sql query to Eloquent relation in laravel?

Good afternoon.

I need to get all members ( members ) in the games played.

select
    members.id,
    members.game_id,
    members.profile_id
from
    members
where
    members.id in (
        select
            games.id
        from
            games
        left join members on games.id = members.game_id
        left join profiles on profiles.id = members.profile_id
        where profiles.id = '4'
    ) and member.profile_id != '4';


Relationships are:

Profiles ( profiles ) <- Members ( ligamentous ) -> Games ( games )

How can I get the same set of data through belongs relative to the current Profile model ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Anton, 2020-11-24
@Fragster

The original SQL query at first glance returns some kind of game:

members.id in (
        select
            games.id
        from
            games

To obtain ITS results, it is not clear what needs to be done. For getting all game participants look at https://laravel.com/docs/8.x/eloquent-relationship... , for selection in related tables: https://laravel.com/docs/8.x/eloquent- relationship...

I
Ilya S, 2020-11-24
@Stalinko

In the model , you Profiledeclare a many-to-many relationship with the model . Game
Then you can use $profile->games.
https://laravel.com/docs/8.x/eloquent-relationship...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question