SORU
11 EKİM 2013, Cuma


Laravel Beliğ : fıkra sorgu birden çok

Laravel Beliğ sorgu builder kullanıyorum ve çok şeyler üzerinde WHERE maddesi istediğim yere sorgu var. Çalışır, ama zarif değil.

Örnek:

$results = User::
      where('this', '=', 1)
    ->where('that', '=', 1)
    ->where('this_too', '=', 1)
    ->where('that_too', '=', 1)
    ->where('this_as_well', '=', 1)
    ->where('that_as_well', '=', 1)
    ->where('this_one_too', '=', 1)
    ->where('that_one_too', '=', 1)
    ->where('this_one_as_well', '=', 1)
    ->where('that_one_as_well', '=', 1)
    ->get();

Daha iyi bir yolu bunu yapmak için vardır, ya da bu yöntem ile sopa gerekir?

CEVAP
17 Aralık 2014, ÇARŞAMBA


Haziran 2014 yılından bu yana where bir dizi iletebilirsiniz

wheres 8 ** operatörü ile tüm olmasını istediğiniz sürece, onları bu şekilde gruplayabilirsiniz:

$matchThese = ['field' => 'value', 'another_field' => 'another_value', ...];

// if you need another group of wheres as an alternative:
$orThere = ['yet_another_field' => 'yet_another_value', ...];

Sonra:

$results = User::where($matchThese)->get();

// with another group
$results = User::where($matchThese)
    ->orWhere($orThere)
    ->get();

Yukarıdaki gibi bir sorgu neden olur:

SELECT * FROM users
  WHERE (field = value AND another_field = another_value AND ...)
  OR (yet_another_field = yet_another_value AND ...)

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Break

    Break

    10 Aralık 2005
  • Michael Neal

    Michael Neal

    2 Mayıs 2009
  • Santozz Yazz

    Santozz Yazz

    23 Mart 2014