Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jagdishabhiandroid's avatar

Database query

i want to know how to apply where with AND condition in following query what am missing

 public function addfav(Request $request)
    {
      
      
      //echo $request->student_id;
      
      if (favourites::where('student_id', $request->student_id)->where('quiz_id',  $request->quiz_id)->exists()) {
          echo 'studentid present';
           
          
          
          
      }else{
      $student = new favourites;
      $student->quiz_name=$request->quiz_name;
      $student->quiz_id=$request->quiz_id;
      $student->student_id=$request->student_id;
      $student->save();
 return Response::json(['message' => 'Quiz added to favourite list']);
       
    }
    ```
0 likes
3 replies
AlexDemin's avatar
Level 16

Your query looks to be correct.

But you can also try to pass an array of conditions to the where function:

$has_favourites = favourites::where([
    ['student_id', $request->student_id],
    ['quiz_id', $request->quiz_id],
])->exists();

See https://laravel.com/docs/5.6/queries#where-clauses

Also, you can debug your query. Put this code before your query:

DB::listen(function ($query){
    dump($query->sql);
    dump($query->bindings);
    dump($query->time);
});
1 like
Snapey's avatar

chaining where's does create AND between them

I would recommend Laravel Debugbar in order to see all your queries

1 like

Please or to participate in this conversation.