Laravel来自给定数组的查询中的多个where子句 [英] Laravel multiple where clauses in query from given array

查看:195
本文介绍了Laravel来自给定数组的查询中的多个where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望标题足够好地描述我的问题. 我试图在laravel中创建地理搜索功能.这些查询本身是正确的.现在,我尝试从表中获取所有与先前查询的邮政编码匹配的文章.我在这里使用的所有功能都可以在这里找到: Laravel 5在foreach中将查询结果添加到数组中).但是现在我想在多个或动态的where子句(带有or)中执行一个查询. 我以前的查询的print_r($zipcodes)(从邮政编码$zipcodes = $this->getZipcodes($zipCoordinateId, $distance);获取范围内的所有邮政编码)输出:

I hope the title describes my problem good as enough. I tried to make a geosearch-function in laravel. The queries as its own are correct. Now I try to get all articles from my table, whose match with the gotten zipcode of a former query. All functions I use you can found here: Laravel 5 add results from query in a foreach to array). But now I want to perform one query, within multiple or dynamic where clauses (with or). The print_r($zipcodes) of my former query (get all zipcodes in a range from a zipcode $zipcodes = $this->getZipcodes($zipCoordinateId, $distance);) outputs:

Array
(
[0] => stdClass Object
    (
        [zc_zip] => 13579
        [distance] => 0
    )

[1] => stdClass Object
    (
        [zc_zip] => 12345
        [distance] => 2.228867736739
    )

[2] => stdClass Object
    (
        [zc_zip] => 98765
        [distance] => 3.7191570094844
    )
)

当我想执行以下操作时,我在laravel中的查询应该如何显示?

So how should my query in laravel should look, when I want to perform following?

SELECT *
FROM articles
WHERE zipcode = '13579'
OR zipcode = '98765'
OR zipcode = '12345';

预先感谢您, 量子论者

Thank you in advance, quantatheist

更新

使用Balantant的溶液可以正常工作.这是我的代码:

With the solution from balintant this is working fine. Here is my code:

// grabs all zipcodes matching the distance
$zipcodes = $this->getZipcodes($zipCoordinateId, $distance);

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

$codes = array_column($zipcodes, 'zc_zip');

$articles = Article::whereIn('zipcode', $codes)->get();

return view('pages.intern.articles.index', compact('articles'));

推荐答案

您可以同时使用whereInorWhere范围.第一个更适合您当前的示例.另外,您可以使用array_column从上面的数组中获取所有真实的邮政编码.

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

更新:

当您要使用array_column获取数组的特定子值时(例如zc_zip),必须首先将其子级转换为数组. 如果是模型,则必须使用toArray() 轻松对其进行转换.

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)
// > }

这篇关于Laravel来自给定数组的查询中的多个where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆