[英]Specifying a select clause in Laravel's eloquent query
Tables 表
category category_path
--------------- ----------------
id category_id
title path_id
level
Queries 查询
CategoryPath::with('Category')
->select('*', DB::Raw('group_concat(title ORDER BY level SEPARATOR " > ") as name'))
->groupBy('category_path.category_id')->paginate(10);
I get an error Unknown column 'title'
in group_concat
. 我在
group_concat
收到错误Unknown column 'title'
。
How can I make selections from the related table? 如何从相关表格中进行选择?
Simple Solution: 简单解决方案
Use model class instead of DB class then query will maintain eloquent relationships: 使用模型类而不是DB类,然后查询将保持雄辩的关系:
$categories = CategoryPath::select('*', DB::Raw('group_concat(title ORDER BY level SEPARATOR " > ") as name'))
->leftJoin('category', 'category_path.path_id', '=', 'category.id')
->groupBy('category_path.category_id')
->orderBy('name', 'ASC')
->paginate(10);
You could use the lists()
method. 您可以使用
lists()
方法。
$categoryPath = CategoryPath::with('category')->paginate(10);
foreach($categoryPath as $path) {
echo implode(' > ', $path->category->lists('title'));
}
That should have the desired affect, although I am making some assumptions. 虽然我做了一些假设,但这应该会产生预期的影响。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.