Jefsama Jefsama - 1 month ago 6
SQL Question

Laravel working out with multiple data in a column

I am working on a laravel base profiling system.

Example tables..

Person

id | Name | sports_id
1 | foo | 1,2
2 | bar | 2,3
3 | derp | 1,3


Sports

id | Name |
1 | basketball |
2 | volleyball |
3 | swimming |


Question:

How can i get all person's name, who's sports are basketball only?

Coz i'm confuse on how to execute the query above due to the
person
's table is not normalize.

Or

Is there a way to make the person table normalize, so i can query the on it properly?

Answer

You should really use many-to-many relationshsip here.

If for some reason you don't want to then use simple query:

User::where('sports_id', 2)->get();