title: How to use if-else on where clause
body: <p>I'm trying to figure out how to use if-else on a where clause when lingkup equals &quot;desa.&quot; Where clause only status = valid but when lingkup is &quot;kecamatan&quot; where clause status = valid and status_kecamatan = valid.</p>
  <pre><code>$results = DB::table('tb_lapak')
      -&gt;select('tb_surat.lingkup', 'tb_lapak.*')
      -&gt;join('tb_surat', 'tb_lapak.kode_surat', '=', 'tb_surat.kode_surat')
      -&gt;when('lingkup' === 'desa', function ($query) {
          return $query-&gt;where('status', '=', 'valid');
      })
      -&gt;when('lingkup' === 'kecamatan', function ($query) {
          return $query-&gt;where('status', '=', 'valid');
          return $query-&gt;where('status_kecamatan', '=', 'valid');
      });
  </code></pre>
  <p>here is the table</p>
  <pre><code>CREATE TABLE `tb_lapak` (
    `kode_lapak` int(11) NOT NULL,
    `kode_surat` int(11) NOT NULL,
    `tanggal` char(35) NOT NULL,
    `nik` char(35) NOT NULL,
    `keterangan` text NOT NULL,
    `status` enum('proses','valid') NOT NULL,
    `status_kecamatan` enum('proses','valid') NOT NULL,
    `create_at` timestamp NULL DEFAULT current_timestamp(),
    `kode_lingkup` int(11) DEFAULT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  
  CREATE TABLE `tb_surat` (
    `kode_surat` int(11) NOT NULL,
    `lingkup` enum('kecamatan','desa') NOT NULL,
    `nama_surat` varchar(225) NOT NULL,
    `output` varchar(225) DEFAULT NULL,
    `aktif` enum('Y','T') NOT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  </code></pre>