【问题标题】:Laravel 5.8 - Get values from pluck resultLaravel 5.8 - 从采摘结果中获取值
【发布时间】:2019-06-11 15:09:55
【问题描述】:

我需要帮助才能使这段代码正常工作。通过软件搜索后,我的控制器返回安装了该软件的本地人,以及 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 的本地人展示??

Tks

【问题讨论】:

    标签: laravel laravel-5 foreach laravel-5.8 pluck


    【解决方案1】:

    如你所说,这个

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

    应该像这样返回一个包含两个 id 的数组:

    [1, 2]
    

    如果这是回复,那么您可以使用whereIn

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

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

    【讨论】:

    • 谢谢老兄!在哪里工作。
    【解决方案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 应该包含与初始查询返回的所有 imagem_id 值匹配的 Ambiente 记录。

    【讨论】:

    • 谢谢老兄!在哪里工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 2019-10-12
    • 2017-07-26
    • 1970-01-01
    • 2019-11-06
    • 2021-09-27
    相关资源
    最近更新 更多