[英]query to laravel query builder
i am building a some of news website an i want to know how many sub categories there in a category. 我正在建立一些新闻网站,我想知道一个类别中有多少个子类别。 I build this query.
我建立这个查询。 (it works on my database) now i want i convert it to laravel query builder but i can't get it working.
(它适用于我的数据库)现在我想将其转换为laravel查询生成器,但无法正常工作。
Raw query: 原始查询:
SELECT news_categories.id,
news_categories.name,
news_categories.description,
count(nc.id)
FROM happyalphen.news_categories
LEFT JOIN news_categories nc ON nc.category_parent = news_categories.id
WHERE news_categories.category_parent = 0
GROUP BY news_categories.id ;
what i have now 我现在有什么
DB::table('news_categories')
->selectRaw('news_categories.id')
->join('news_categories subCat', 'news_categories.category_parent', '=', 'subCat.id')
->where('news_categories.category_parent','=',0)
->groupBy('news_categories.id')
->get();
Table layout 表格布局
i found it (Thanks Jarek Tkacyk) 我找到了(谢谢Jarek Tkacyk)
i was forgot the 'AS' in the join that laravel stop with the query. 我忘记了laravel在查询中停止的联接中的“ AS”。
DB::table('news_categories')
->selectRaw(' `news_categories`.`id`, `news_categories`.`name`, `news_categories`.`description`, `news_categories`.`color`, `news_categories`.`text_color`, count(`SubCat`.`id`) as SubCatCount ')
->join('news_categories as subCat', 'subCat.category_parent', '=', 'news_categories.id','left')
->where('news_categories.category_parent','=','0')
->groupBy('news_categories.id')
->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.