简体   繁体   中英

How to create condition based query using Eloquent ORM Laravel?

Can I use this code with if condition like this?

$testq= DB::table('attendances')
    if($flag==1)
    ->where('type', '=', $userinput)
    else
    ->where('type', '=', $newdate)
    ->get();

You can. This will work for you:

$query = DB::table('attendances');

if($flag == 1) {
  $query->where('type', '=', $userinput)
} else {
  $query->where('type', '=', $newdate)
}

$result = $query->get();

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