【问题标题】:Transform collective to array in Laravel, Laravel 5在 Laravel,Laravel 5 中将集合转换为数组
【发布时间】:2018-02-16 15:28:04
【问题描述】:

如何将此集合转换为数组并使用 Laravel 查询弹出“whereNotIn”,如下所示:

->whereNotIn('id', ['collection'])->get();'


Collection {#259 ▼
#items: array:3 [▼
0 => {#257 ▼
  +"id": 2
}
1 => {#256 ▼
  +"id": 3
}
2 => {#237 ▼
  +"id": 6
}
]}

【问题讨论】:

  • 您的问题到底是什么?如果要转换为数组,只需使用 toArray 方法
  • 我想使用 'whereNotIn' 方法,但我不知道如何将 laravel coletion 传递给数组 ->whereNotIn('id', [array])->get();'跨度>
  • ->get()->toArray() ?

标签: php laravel laravel-5 eloquent laravel-collection


【解决方案1】:

使用pluck(attribute):

->whereNotIn('id', $collection->pluck('id'))->get();

【讨论】:

    【解决方案2】:

    其实要得到一个数组,你应该使用pluckall()方法,所以在这种情况下你应该使用:

    ->whereNotIn('id', $collection->pluck('id')->all())->get();
    

    【讨论】:

      猜你喜欢
      • 2018-08-11
      • 2016-05-12
      • 2019-08-10
      • 2017-06-30
      • 2019-01-05
      • 2019-07-05
      • 1970-01-01
      相关资源
      最近更新 更多