0

我需要帮助才能使此代码正常工作。通过软件进行搜索后,我的控制器返回安装了该软件的本地人,以及安装了该软件的 SO。例如,此查询将返回“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 的当地人展示?

Tks

4

2 回答 2

4

正如你所说,这

$img_id = Software::where('application', $request->input('application'))->pluck('imagem_id');

应该返回两个 id 的数组,如下所示:

[1, 2]

如果这是响应,那么您可以使用whereIn

$ambientes = Ambiente::whereIn('imagem_id', $img_id)->get();

这将为您提供两种环境的集合,并且视图保持不变。

于 2019-06-11T15:13:15.717 回答
2

当您尝试获取匹配多个 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

于 2019-06-11T15:14:09.413 回答