Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

KonScyence's avatar

MySQL Query

Hi, I would like to put a condition in a query, but I do some things wrong, what is it?

$id = 1;

$groups = DB::table('table_example')
      if($id!='0'){
          ->where('id', '=', $id)
         }
      ->limit(30)
      ->get();

Best regards

0 likes
2 replies
wilburpowery's avatar
Level 23
$id = 1;

$groups = DB::table('table_example');

 if($id!='0') {
     $groups->where('id', '=', $id)
 }

  $groups->limit(30)->get();

Try that.

1 like

Please or to participate in this conversation.