Laravel multiple where clauses in query from given array

℡╲_俬逩灬. 提交于 2019-12-06 14:10:25

You can use both the whereIn and orWhere scopes. The first one better fits to your current example. Also, you can use array_column to get all the real zip codes from the array above.

$query->whereIn('zip', [12,34,999])->get();
// > array

Update:

When you want to use array_column to get the specific subvalues of the array (like zc_zip) you must first transform it's childs to an array. If it's a model you must transform it easily with toArray().

$zip_objects = [
    (object) [ 'zc_zip' => 13579, 'distance' => 0 ],
    (object) [ 'zc_zip' => 12345, 'distance' => 2.228867736739 ],
    (object) [ 'zc_zip' => 98765, 'distance' => 3.7191570094844 ],
];

foreach ( $zip_objects AS $key=>$val )
{
    $zip_objects[$key] = (array) $val;
}

$zip_codes = array_column($zip_objects, 'zc_zip');

var_dump($zip_codes);
// > array(3) {
// >  [0]=>
// >  int(13579)
// >  [1]=>
// >  int(12345)
// >  [2]=>
// >  int(98765)
// > }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!