在 Laravel 中使用 IFNULL

9

我在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();

当表中没有相关条目时,is_download会变成null,这是唯一的问题。经过一些研究,我发现有一个函数IFNULL,可以使用它来将is_downloadnull更改为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',  IFNULL( `downloads`.`is_download` , 0 ) )
        ->orderBy('subjectID')
        ->get();

我能在phpmyadmin中编写这个查询,但不知道如何在Laravel中编写。

这是API,所以整个代码看起来像:

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
    ]));
});
3个回答

10

试试这样:

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

空白屏幕。没有变化。 - silentcoder
你是否使用了 \DB::table() 而不是 $app->db->table() 和 \DB::Raw() ? 这应该会正常工作。否则,在使用laravel时,请再次检查您的查询。 - Md Mahfuzur Rahman
在 Laravel 文档中,我没有看到类似这样的内容。所以最好使用 DB::table()。 - Md Mahfuzur Rahman

4
您只需要像这样使用:
DB::raw('IFNULL( downloads.is_download, 0) as is_download')

这对我的情况有所帮助,谢谢。我有一个需要这个的场景,还有其他选择。例如,$colArray = [ 'column1' , 'otherColumn' ,DB::raw('IFNULL( downloads.is_download, 0) as is_download') ]; - kinsley kajiva

0

这是我的代码

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接