gpt4 book ai didi

mysql - 使用 eloquent 根据结果查找结果

转载 作者:行者123 更新时间:2023-11-29 11:32:05 26 4
gpt4 key购买 nike

我有下表。

地点

id | postcode | suburb | state

还有另一个表:

分类广告

id | name | location - Matches to 'id' in the first table

首先,我有一个代码,它执行表一中的位置结果并将其保存到变量中。接下来,我需要查找哪些分类与变量中的结果匹配。这是我到目前为止所拥有的,不太确定该怎么做。

此代码在 View 上运行:

$locations = Location::where('location', '=', $_GET['location']); (From the URL)

此代码需要在“分类”表中查找与分类的位置 ID 与 $locations 变量中的 ANY ID 相匹配的结果

    foreach($locations as $loc)
{
foreach($classifieds as $classi)
{
if($loc->id == $classi->location)
{

}
}
}

类似这样的^,但这段代码不起作用

用一个例子进一步解释这一点:

位置数组变量具有:

98212 | 4220 | 4220伯利头 |昆士兰州(身份证、邮政编码、郊区、州)

98213 | 4221 |伯利水域 |昆士兰州(身份证、邮政编码、郊区、州)

这是 (Classified::where('location, '=', $_GET['location')->get())

的结果

分类表有:

1 | Red Bike | 98212 (id, name, location)

2 | Red Bike | 98213 (id, name, location)

3 | Red Bike | 98213 (id, name, location)

查找变量中列出位置的分类广告

最佳答案

使用Collection方法pluck()lists()将所有位置ID提取到一个数组中(取决于版本):

$locationIds = $locations->pluck(id);

并使用 Eloquents whereIn() 方法:

$classfields = Classified::whereIn('location', $locationIds)->get();

这一切都可以是一句话:

$classfields = Classified::whereIn('location', $locations->pluck(id))->get();

关于mysql - 使用 eloquent 根据结果查找结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37237207/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com