Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel的雄辩查询中指定select子句_Php_Mysql_Laravel_Eloquent - Fatal编程技术网

Php 在Laravel的雄辩查询中指定select子句

Php 在Laravel的雄辩查询中指定select子句,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,桌子 询问 category category_path --------------- ---------------- id category_id title path_id level 我在组_concat中得到一个未知列“title”错误 如何从相关表格中进行选择 简单解决方案: 使用模型类而不是DB类,

桌子

询问

category                 category_path
---------------          ----------------
id                       category_id
title                    path_id
                         level
我在组_concat中得到一个未知列“title”错误

如何从相关表格中进行选择

简单解决方案:

使用模型类而不是DB类,则查询将保持雄辩的关系:

    CategoryPath::with('Category')
        ->select('*', DB::Raw('group_concat(title ORDER BY level SEPARATOR " > ") as name'))
        ->groupBy('category_path.category_id')->paginate(10);
您可以使用lists方法


虽然我在做一些假设,但这应该会产生预期的效果。

为什么您只想访问某些属性?我需要对类别标题进行分组,以便将其打印为完整路径,如计算机>零件>CPUThis,但我宁愿使用查询生成器编写完整查询。我想知道我是否能用有说服力的关系做到这一点。你有没有试过将组_concat的标题部分改为类别。标题?是的,我试过了。有相同的错误未知列的类别。标题。我认为您最好的选择是我上面建议的方法,或者为此切换到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);
$categoryPath = CategoryPath::with('category')->paginate(10);

foreach($categoryPath as $path) {
    echo implode(' > ', $path->category->lists('title'));
}