Wednesday, March 1, 2017

Relationship of three tables

I have three tables profiles, category and sector, I want to display the first 4 users classified by sector for each category. I have 10 category.

Example: Category: Number of doctor profile in the profile table Doctor 120

sector: Number of profile in each sector, Dentist 35 Gynecologist 29 Cardiologist 24 Dermatologist 13

in below query :

$list = \DB::table('profiles')
        ->select(\DB::raw('count(*) as nb, category.titre, sector.titre'))
        ->join('sectors', 'sectors.id', '=', 'profiles.sectors_id')
        ->leftjoin('category', 'category.id', '=', 'sectors.category_id')
        ->groupby('category.titre')
        ->orderBy('nb', 'desc')
        ->get();




via nabil

Advertisement