Php Laravel没有返回我需要的东西
我有一个疑问:Php Laravel没有返回我需要的东西,php,laravel,laravel-5.4,Php,Laravel,Laravel 5.4,我有一个疑问: $postsTags = DB::table('post_tag')->where('tag_id', $id)->get(); if(count($postsTags) > 0) { // Pluck the ID's from a users topics $postIDs = $postsTags->pluck('post_id'); dd($postIDs);
$postsTags = DB::table('post_tag')->where('tag_id', $id)->get();
if(count($postsTags) > 0) {
// Pluck the ID's from a users topics
$postIDs = $postsTags->pluck('post_id');
dd($postIDs);
$posts = Post::whereIn('id', [$postIDs])->get();
}
当我添加$postIDs时,我得到以下信息:
然后我进行where查询,但问题是它只返回一个结果,而不是它们的数组
如果我这样做:
$postIDs[0]
我得到112,但我的结果应该是[111112]。所以这是行不通的
dd($postIDs->all())的结果代码>
正如@Angad Dubey所说,我必须这样做:
$posts = Post::whereIn('id', $postIDs->all())->get();
它返回了我需要的结果。如果要传递集合,则需要传递数组。要么在主查询结束时更改chain->toArray()
,要么使用->flatte()
展平您的集合。我已经尝试过这样做,但我没有将它放到数组中。我可能遗漏了什么,你能在查询中告诉我吗?dd($postIDs->all())的结果是什么代码>我在上面的editTry中添加了它:$posts=Post::其中('id',$postIDs->all())->get()代码>