大家晚上好,我有laravel paginator问题,我的查询包含以下三个请求
public static function getActu($user_id){
// 1 select all the articles published by the shop followed by a user
$res = DB::table('users')
->join('user_folow_boutiques', 'user_folow_boutiques.user_id', '=', 'users.id')
->join('boutique', 'boutique.id', '=', 'user_folow_boutiques.boutique_id')
->join('type_article', 'type_article.boutique_id', '=', 'boutique.id')
->join('article', 'type_article.id', '=', 'article.type_article_id')
->where('users.id','=',$user_id)
->select(DB::raw('article.*,article.created_at as date,null as nbrlik'));
// 2 Select all the products that are liked by the users I followed
$res2 = DB::table('users')
->join('user_follow_user', 'user_follow_user.user_id_1', '=', 'users.id')
->join('user_aime_article', 'user_follow_user.user_id_2', '=', 'user_aime_article.user_id')
->join('article', 'user_aime_article.article_id', '=', 'article.id')
->where('users.id','=',$user_id)
->select(DB::raw('article.*,user_aime_article.created_at as date, null as nbrlik') )
->distinct();
// 3 Select all the products of the shop followed by the user and who have exactly 50 likes
$res3 = DB::table('users')
->join('user_folow_boutiques', 'user_folow_boutiques.user_id', '=', 'users.id')
->join('boutique', 'boutique.id', '=', 'user_folow_boutiques.boutique_id')
->join('type_article', 'type_article.boutique_id', '=', 'boutique.id')
->join('article', 'type_article.id', '=', 'article.type_article_id')
->join(DB::raw('(SELECT article.id c_id, count(user_aime_article.id) as nbrlik FROM article JOIN user_aime_article on user_aime_article.article_id = article.id GROUP BY user_aime_article.article_id) e'),'article.id','=','e.c_id')
->where('users.id','=',$user_id)
->where('e.nbrlik','=',50)
->select(DB::raw('article.*,article.created_at as date, e.nbrlik'))
->union($res)
->union($res2)
->groupBy('article.id')
->orderBy('date','desc')
->paginate(20)
->get();
return $res3;
}
我需要此信息才能在客户的站点中创建新闻提要,并且需要分页来创建无限滚动,但这不起作用
我在网上搜索了此问题,发现无法将paginate (x)
方法与groupby一起使用,并且要解决此问题,我必须创建分页,并且尝试了以下操作,但确实如此不起作用
$paginate = 5;
$list_news = User::getActu(Auth::user()->id);// without ->paginate(20);
$slice = array_slice($list_news->toArray(), $paginate * ($page - 1), $paginate);
$result = new Paginator($slice, 20);
答案 0 :(得分:0)
解决问题的方法,请稍等片刻,以comme套件为准:
public static function getActu($user_id,$page){
$paginate = 5;
// 1 selectionnée tous les article publier par les shop suivi
$res = DB::table('users')
->join('user_folow_boutiques', 'user_folow_boutiques.user_id', '=', 'users.id')
->join('boutique', 'boutique.id', '=', 'user_folow_boutiques.boutique_id')
->join('type_article', 'type_article.boutique_id', '=', 'boutique.id')
->join('article', 'type_article.id', '=', 'article.type_article_id')
->where('users.id','=',$user_id)
->select(DB::raw('article.*,article.created_at as date,null as nbrlik'));
// 2 Selectionnée tous les produit amai par les utilisateur suivit Arevoir demain
$res2 = DB::table('users')
->join('user_follow_user', 'user_follow_user.user_id_1', '=', 'users.id')
->join('user_aime_article', 'user_follow_user.user_id_2', '=', 'user_aime_article.user_id')
->join('article', 'user_aime_article.article_id', '=', 'article.id')
->where('users.id','=',$user_id)
->select(DB::raw('article.*,user_aime_article.created_at as date, null as nbrlik') )
->distinct();
// Selectioner tous les proiduit des boutique suivi par le user qui ont 50 j'aime
$res3 = DB::table('users')
->join('user_folow_boutiques', 'user_folow_boutiques.user_id', '=', 'users.id')
->join('boutique', 'boutique.id', '=', 'user_folow_boutiques.boutique_id')
->join('type_article', 'type_article.boutique_id', '=', 'boutique.id')
->join('article', 'type_article.id', '=', 'article.type_article_id')
->join(DB::raw('(SELECT article.id c_id, count(user_aime_article.id) as nbrlik FROM article JOIN user_aime_article on user_aime_article.article_id = article.id GROUP BY user_aime_article.article_id) e'),'article.id','=','e.c_id')
->where('users.id','=',$user_id)
->where('e.nbrlik','=',50)
->select(DB::raw('article.*,article.created_at as date, e.nbrlik'))
->union($res)
->union($res2)
->groupBy('article.id')
->orderBy('date','DESC')
->skip($paginate * ($page - 1) )->take($paginate)->get();
return $res3;
}
感谢@N Mahurin
答案 1 :(得分:0)
联合和分页不能一起使用 您可以尝试这个。
$res3 = DB::table('users')
->join('user_folow_boutiques', 'user_folow_boutiques.user_id', '=', 'users.id')
->join('boutique', 'boutique.id', '=', 'user_folow_boutiques.boutique_id')
->join('type_article', 'type_article.boutique_id', '=', 'boutique.id')
->join('article', 'type_article.id', '=', 'article.type_article_id')
->join(DB::raw('(SELECT article.id c_id, count(user_aime_article.id) as nbrlik FROM article JOIN user_aime_article on user_aime_article.article_id = article.id GROUP BY user_aime_article.article_id) e'),'article.id','=','e.c_id')
->where('users.id','=',$user_id)
->where('e.nbrlik','=',50)
->select(DB::raw('article.*,article.created_at as date, e.nbrlik'))
->union($res)
->union($res2)
->groupBy('article.id')
->orderBy('date','DESC');
$sql = $res3->toSql();
$result = \DB::table(\DB::raw("($sql) as a"))->mergeBindings($res3)
->Paginate(10);