我需要帮助才能使此代码正常工作。通过软件进行搜索后,我的控制器返回安装了该软件的当地人,以及 S.O.谁有相同的。 例如,此查询将返回“1 2”,因为这两个图像具有“Google Chrome”。
$img_id = Software::where('application', $request->input('application'))->pluck('imagem_id');
然后,在软件搜索后,我需要列出所有拥有此图像的当地人。
$ambientes = Ambiente::where('imagem_id', $img_id)->get();
但她只向我展示 imagem_id = 1 的当地人,即使我在视图中使用 foreach :
@foreach ($ambientes as $value)
<tr>
<td>{{ $value->unidade->name }}</td>
<td>{{ $value->bloco->name }}</td>
<td>{{ $value->name }}</td>
<td>{{ $value->imagem_id }}</td>
</tr>
@endforeach
我需要做什么才能向每个当地人显示 image_id = 1 和 2?
正如你所说,这个
$img_id = Software::where('application', $request->input('application'))->pluck('imagem_id');
应该返回两个 id 的数组,如下所示:
[1, 2]
如果这是响应,那么您可以使用 whereIn:
$ambientes = Ambiente::whereIn('imagem_id', $img_id)->get();
这将为您提供两种环境的集合,并且视图保持不变。
当您尝试获取与多个 ID 匹配的记录时,您不能使用
where()
,您需要使用 whereIn()
:
$img_id = Software::where('application', $request->input('application'))->pluck('imagem_id');
// This should return an array/Collection of `imagem_id` values.
$ambientes = Ambiente::whereIn('imagem_id', $img_id)->get();
现在,
$ambientes
应包含与初始查询返回的所有 Ambiente
值相匹配的 imagem_id
记录。