articleのデータをdaily_ranking配列に入っているidの順番に取り出したいのですが、どのようにすればよいでしょうか?自分で作ってみたのですが、うまくいきません。ご教授お願いします。
public function index() { $ranking = new RankingModule; $daily_ranking = $ranking->get_daily_ranking(); #daily_rankingはarray('articleのid' => 'pv数','articleのid' => 'pv数',......)となっており、pv数が多い順に並んでいる。 #下記でArticleのデータをpv数の多い順に並べたい $articles_ranking_array = Array(); foreach($daily_ranking as $key => $value){ $articles_ranking_array[] = Article::findOrFail($key); } return view('articles.index', ['articles' => $articles, '$articles_ranking_array' => $articles_ranking_array]); }
RaningModele.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Redis; class RankingModule extends Controller { public function increment_view_ranking($id) { $key = "article/" . date('Ymd') . "/" . $id; $value = Redis::get($key); if(empty($value)){ Redis::set($key, 1); }else{ Redis::set($key, $value + 1); } } public function get_daily_ranking() { $keys = Redis::keys('article/' . date('Ymd') . '/*'); $results = Array(); if(empty($keys) != true){ for($i=0; $i<sizeof($keys); $i++){ $point = Redis::get($keys[$i]); $id = explode('article/' . date('Ymd') . '/', $keys[$i])[1]; $results[$id] = $point; } arsort($results, SORT_NUMERIC); } return $results; } }
参考にしているサイトです
https://qiita.com/esu_eichi/items/e3abe72f55b53865acf5
あなたの回答
tips
プレビュー