Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.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 雄辩者有许多没有身份的关系_Php_Sql_Laravel_Eloquent_Relationship - Fatal编程技术网

Php 雄辩者有许多没有身份的关系

Php 雄辩者有许多没有身份的关系,php,sql,laravel,eloquent,relationship,Php,Sql,Laravel,Eloquent,Relationship,我想数一数同一个国家有多少注册。 到目前为止,我提出了以下解决方案: 控制器内的方法: class-Camp扩展模型//。。。 { 受保护的$with=['注册\国家\计数',…]; // ... 公共功能注册\国家\计数() { 返回$this->hasMany(CampRegistration::class) ->选择(['camp_id','nation',DB::raw('COUNT(*)as COUNT')) ->groupBy(“国家”); } } 这将生成以下输出:

我想数一数同一个国家有多少注册。 到目前为止,我提出了以下解决方案: 控制器内的方法:

class-Camp扩展模型//。。。
{
受保护的$with=['注册\国家\计数',…];
// ...
公共功能注册\国家\计数()
{
返回$this->hasMany(CampRegistration::class)
->选择(['camp_id','nation',DB::raw('COUNT(*)as COUNT'))
->groupBy(“国家”);
}
}
这将生成以下输出:

      "registrations_nation_count": [
            {
                "camp_id": 1,
                "nation": "en",
                "count": 2
            },
            {
                "camp_id": 1,
                "nation": "fr",
                "count": 1
            }
        ]
现在我想从输出中删除camp_id列。我已经试着简单地从select中删除它,但是这导致根本没有输出。 有没有办法把它藏起来

数据库:

+-------+--------------------+ | camps | camp_registrations | +-------+--------------------+ | id | id | | ... | camp_id | | | nation | | | ... | +-------+--------------------+ +-------+--------------------+ |营地|营地|注册| +-------+--------------------+ |id | id| | ... | 营地id| ||国家| | | ... | +-------+--------------------+
您可能希望执行以下操作:

$camp->registrations\u nation\u count->makeHidden('camp\u id');

使用“with”后,不能选择用于查询关系的列。在您的示例中,这是父模型的
id
和相关模型的
camp\u id

我强烈建议不要在关系定义中使用
->select([…])
列。这将不必要地限制关系的使用


相反,您应该省略某种转换器(例如)中的列,该转换器转换资源以供最终用户查看。

Yes。它会导致输出为空<代码>“registrations\u nation\u count”:[]啊,这个方法registrations\u nation\u count()是否在您的营地模型中?是的,完全正确。我编辑了问题并添加了类定义和重要变量。我理解资源背后的概念,但如果不使用select,我如何计算同一国家的所有注册?