简体   繁体   中英

how to check if one column field data is null then check other field that same row laravel eloquent

I am Retrieve Data From MySQL Database, where the condition is if the checking out data field is null then check the send field to that particular row

id   |  SSC | hhc |  dip
1    |  50  | 50  | null
2    |  60  | null| 60
3    |  50  | 55  | null

condition is "hhc" and "dip" are not both null and they are not both fill up at one time

i realy don't know what to do i try

$users->where('student_masters.hhc','>=' ,60);
$users->where('student_masters.dip','>=' ,60);

i retrive all data but condition is if('student_masters.hhc' == null) then check the other field means student_masters.dip please help me what i do

   ->where(function ($q) {
        $q->where('student_masters.hhc','!=' ,null);
        $q->where('student_masters.dip','=' ,null);
    })
    ->orWhere(function ($q) {
        $q->where('student_masters.hhc','=' ,null);
        $q->where('student_masters.dip','!=' ,null);
    })

This may help you. though question is not clear to me.

If you just want to pull the value of either column and don't care which one it came from, you could use a MySQL case statement. That will allow you to use the value of dip if hhc is null.

$users->select( DB::raw("CASE WHEN hhc IS NULL THEN dip ELSE hhc END AS data_value") )->get();

Note that we're using a raw select statement here as there is no way to do this with pure query builder.

https://laravel.com/docs/5.8/database#running-queries

https://dev.mysql.com/doc/refman/5.7/en/case.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM