Ho una query in laravel
che funziona correttamente.utilizza IFNULL in 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();
C'è solo un problema che viene is_download
null quando non v'è alcuna voce relativa nella tabella. Dopo alcune ricerche ho scoperto che esiste una funzione IFNULL
utilizzando la quale posso modificare is_download
null
a 0
. Quindi ecco la mia query che non funziona. Lo schermo vuoto sta mostrando.
$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();
Sono in grado di scrivere questa query nella mia phpMyAdmin, ma non sanno come scrivere in laravel
Questa è l'API, così tutto il codice è simile
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
]));
});
schermo vuoto. nessun cambiamento. –
Hai usato \ DB :: table() invece di $ app-> db-> table() e \ DB :: Raw()? Questo dovrebbe funzionare bene. Altrimenti controlla di nuovo la tua query quando la stai usando in laravel. –
cosa non va usando "$ app-> db-> table()'? –