【问题标题】:Using Array in '->where()' for Laravel Query Building在 '->where()' 中使用数组来构建 Laravel 查询
【发布时间】:2015-12-16 20:25:04
【问题描述】:

我正在尝试获取列的所有值,然后在另一个查询中使用它。我使用lists 并将值作为数组获取,但是我找不到在其他查询中检查该变量(包含每个 id 的数组)的方法

$subscribes = Subscribe::where('from_id', $currentUser)->lists('id')

// dd($subscribes), logs values as array.


$videos = Photo::where('id', $subscribes)->get();

这不起作用,因为 $subscribes 是一个数组。

我应该使用 for 循环并为每个 id 发送另一个查询吗?还是有一种我错过的实用方法?正确的使用方法是什么?

【问题讨论】:

  • 它低于一个工作吗?

标签: php mysql laravel


【解决方案1】:

使用whereIn方法:

$videos = Photo::whereIn('id', $subscribes)->get();

http://laravel.com/docs/5.1/queries(向下滚动/搜索“whereIn”)

【讨论】:

    【解决方案2】:

    应用whereIn方法

    if(is_array($subscribes) && count($subscribes) > 0){
        $videos = Photo::whereIn('id', $subscribes)->get();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-09
      • 2015-06-03
      • 1970-01-01
      • 2021-11-16
      • 2019-01-18
      • 1970-01-01
      • 2017-01-14
      • 2019-09-20
      相关资源
      最近更新 更多