【发布时间】:2013-12-18 01:30:57
【问题描述】:
表格
category category_path
--------------- ----------------
id category_id
title path_id
level
查询
CategoryPath::with('Category')
->select('*', DB::Raw('group_concat(title ORDER BY level SEPARATOR " > ") as name'))
->groupBy('category_path.category_id')->paginate(10);
我在group_concat 中收到错误Unknown column 'title'。
如何从相关表中进行选择?
简单的解决方案:
使用模型类而不是数据库类然后查询将保持雄辩的关系:
$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);
【问题讨论】:
-
为什么只想访问某些属性?
-
我需要 group_concat 类别标题,以便打印为完整路径,如
Computers > Parts > CPU
标签: php mysql laravel eloquent