2016-03-22 11 views
6

Mam zapytanie w laravel, który działa poprawnie.używać IFNULL w laravel

$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(); 

Jest tylko jeden problem, który is_download pochodzi NULL, gdy nie ma względna pozycja w tabeli. Po pewnych badaniach odkryłem, że istnieje funkcja IFNULL, za pomocą której mogę zmienić is_downloadnull na na . Oto moje zapytanie, które nie działa. Wyświetli się pusty ekran.

$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(); 

jestem w stanie napisać tej kwerendy w moim phpmyadmin, ale nie wiem jak napisać w laravel

Jest to API, więc cały kod wygląda

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 
    ])); 
}); 

Odpowiedz

3

Spróbuj tak:

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(); 
+0

pusty ekran. bez zmiany. –

+0

Czy używałeś \ DB :: table() zamiast $ app-> db-> table() i \ DB :: Raw()? To powinno działać dobrze. W przeciwnym razie sprawdź ponownie zapytanie, gdy używasz go w laravel. –

+0

co jest złego w użyciu '$ app-> db-> table()'? –