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']);
}
```
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);
});
chaining where's does create AND between them
I would recommend Laravel Debugbar in order to see all your queries
thanks for reply issue is resolved
Please or to participate in this conversation.