Reputation: 359
I'm trying to pass an array of lists of post_id from post_views table to posts table. What I got so far is, when I pass the $newsTagRetreived array to $posts, it only gives me first posts. Current code looks like this:
$newsTagRetreived = NewsTag::where('created_at', '>',
\Carbon\Carbon::now()->subDay()->toDateTimeString())
->orderBy('views','desc')
->pluck('post_id')
->implode(', ');
// dd($newsTagRetreived);
this returns me exactly all lists of posts_id I need in exact order I need but only 1 post.
In the post,
$posts = Post::where('post_status', 'publish')
->where('post_type', 'post')->whereIn('ID',[$newsTagRetreived])->paginate('12');
How do I get all posts in the same order as passed by $newsTagRetreived in $posts? Thank you!
Upvotes: 0
Views: 31
Reputation: 4813
//...
->orderByRaw(\DB::raw("FIELD(ID, ". implode(",",$newsTagRetreived) ." )"))->paginate('12');
//...
See http://oldblog.codebyjeff.com/blog/2015/02/laravel-quick-tip-order-by-raw
Upvotes: 1