if(null) php_php – 在laravel中使用IFNULL

我在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只有一个问题.经过一些研究,我发现有一个函数IFNULL使用我可以将is_download null更改为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

]));

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值