skip to Main Content

Im having a hard time converting this raw MySQL query in phpmyadmin. I want to convert it to laravel’s query builder or eloquent. I already configured the relationships in the model. Can someone convert my query to querybuilder and eloquent? I tried some converter but it doesnt work for me. Thanks

Raw Query

SELECT count( * ) FROM users u, sectionschedules s, notes n 
WHERE u.parent_id=26 
AND u.section_id=s.section_id 
AND s.id = n.schedule_id;

Query I tried

$notes = DB::table('notes')
                    ->join('users', 'users.section_id', '=', 'notes.section_id')
                    ->join('sections','sections.section_id', '=', 'sectionschedules.section_id')
                    ->join('sectionschedules', 'sectionschedules.id','=','notes.schedule_id')
                    ->where('user.parent_id', '=', 26)
                    ->count();

Error I got

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user.parent_id' in 'where clause

2

Answers


  1. Try

      $notes = DB::table('sectionschedules')
               ->select('notes.*','sectionschedules.*','users.*')
               ->join('users','users.section_id', '=', 'sectionschedules.section_id')
               ->join('notes', 'sectionschedules.id','=','notes.schedule_id')
               ->where('users.parent_id', '=', 26)
               ->count();
    
    Login or Signup to reply.
  2. $notes = DB::table('notes')
                    ->join('users', 'users.section_id', '=', 'notes.section_id')
                    ->join('sections','sections.section_id', '=', 'sectionschedules.section_id')
                    ->join('sectionschedules', 'sectionschedules.id','=','notes.schedule_id')
                    ->where('users.parent_id', '=', 26)
                    ->count();
    

    Users.parent id as defined not user.parent_id

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search