10

I have a query in laravel which is working fine.

$subjects = $app->db->table('subjects')->LeftJoin('downloads', 'subjects.subjectID', '=', 'downloads.subject_id')
        ->where('universityID', $currentUser->universityID)->where('semesterID', $currentUser->semesterID)->where('courseID', $currentUser->courseID)
        ->select('subjects.subjectID', 'subjects.subjectName', 'subjects.price',  'downloads.is_download' )
        ->orderBy('subjectID')
        ->get();

There is only one issue that is_download comes null when there is no relative entry in table. After some research I found that there is a function IFNULL by using which I can change is_download null to 0. So here is my query which is not working. Blank screen is showing.

$subjects = $app->db->table('subjects')->LeftJoin('downloads', 'subjects.subjectID', '=', 'downloads.subject_id')
        ->where('universityID', $currentUser->universityID)->where('semesterID', $currentUser->semesterID)->where('courseID', $currentUser->courseID)
        ->select('subjects.subjectID', 'subjects.subjectName', 'subjects.price',  IFNULL( `downloads`.`is_download` , 0 ) )
        ->orderBy('subjectID')
        ->get();

I am able to write this query in my phpmyadmin but not know how to write in laravel

This is the api, So whole code looks like

use Illuminate\Database\Query\Expression as raw;
use project\Models\Subject;
use project\Models\Semester;
use project\Models\StudentRegistration;

$app->get('/api/getSubject/:studentID', function($studentID) use ($app) {
error_reporting(E_ALL);
    $currentUser = StudentRegistration::where('studentID', $studentID)->first();

$subjects = $app->db->table('subjects')->LeftJoin('downloads', 'subjects.subjectID', '=', 'downloads.subject_id')
        ->where('universityID', $currentUser->universityID)->where('semesterID', $currentUser->semesterID)->where('courseID', $currentUser->courseID)
        ->select('subjects.subjectID', 'subjects.subjectName', 'subjects.price',  IFNULL( `downloads`.`is_download` , 0 ) )
        ->orderBy('subjectID')
        ->get();
print_r($subjects);
    return $app->response->write(json_encode([
                'error' => 0,
                'subjects' => $subjects
    ]));
});
0

3 Answers 3

12

Try like this:

DB::Raw('IFNULL( `downloads`.`is_download` , 0 )');


$subjects = $app->db->table('subjects')->LeftJoin('downloads', 'subjects.subjectID', '=', 'downloads.subject_id')
    ->where('universityID', $currentUser->universityID)->where('semesterID', $currentUser->semesterID)->where('courseID', $currentUser->courseID)
    ->select('subjects.subjectID', 'subjects.subjectName', 'subjects.price',  DB::Raw('IFNULL( `downloads`.`is_download` , 0 )') )
    ->orderBy('subjectID')
    ->get();
Sign up to request clarification or add additional context in comments.

3 Comments

blank screen. no change.
Did you use \DB::table() instead of $app->db->table() and \DB::Raw() ? This should work fine. Otherwise check your query again when you are using it in laravel.
In laravel documentation, I didn't see anything like this. So, it's better to use DB::table().
5

You just need to use like this way,

DB::raw('IFNULL( downloads.is_download, 0) as is_download')

1 Comment

this has helped me in my case thanks i had a scenario that required this case another other selections for example $colArray = [ 'column1' , 'otherColumn' ,DB::raw('IFNULL( downloads.is_download, 0) as is_download') ];
0

This my code

->whereRaw('customer_id = IFNULL(?, customer_id)', [$request->customer_id])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.