通过 laravel 集合格式化数据集
Formatting data set through laravel collection
我有两个模型 Roles
和 Specialisation
,它们之间的关系是 hasMany
和 belongsTo
。我有另一个名为 Company
的模型,它与每个模型都有 Many-to-many
关系。我正在制作一张图表,我需要有如下数据集:
data: {
Consultant: { "Avenger": 14, "Challenger": 647 },
Contractor: { "124 Spider": 4478, "500": 12685, "500L": 1664, "500X": 7665 },
Manufacturer: { "C-Max": 390, "Edge": 1423, "Escape": 308296, "E-Series": 53304, "Expedition": 5183, "Explorer": 2731, "Fiesta": 46249},
Miscellaneous: { "C-Max": 18390, "Edge": 142603, "Escape": 308296, "E-Series": 53304, "Expedition": 883, "Explorer": 2731 },
Owner: { "C-Max": 18390 },
Supplier: { "Expedition": 5883, "Explorer": 271131, "Fiesta": 46249, "Flex": 2289, "Focus": 1583 },
}
这里 Consultant
, Contractor
, Manufacturer
, ...等代表 role->name
, 在对象的索引里面我有 specialisation->name
和代表与他们相关的公司数量的数字,我想用以下代码实现:
$data = Role::whereNull('parent_id')->get();
$roles = collect($data)->map(function ($role) {
$specialisation = Specialisation::where('parent_id', $role->id)->get();
$element[$role->name] = array(
//specialisation and companies_count
);
return $element;
});
我对实现这个有点困惑。帮我解决这个问题。欢迎提出任何建议。谢谢。
我无法从 collection
方法中找到任何解决方案。为此,我尝试在 stdClass()
的帮助下创建一个对象,然后在对象内部创建另一个对象,然后返回整个数据。像这样:
$data = Role::whereNull('parent_id')->withCount('companies')->get();
$object = new \stdClass();
foreach($data as $key => $value)
{
$name = $value->name;
$specialisation = Specialisation::where('parent_id', $value->id)->withCount('companies')->get();
$ob = new \stdClass();
foreach ($specialisation as $sp => $spec)
{
$n = $spec->name;
$ob->$n = $spec->companies_count;
}
$object->$name = $ob;
}
return response()->json(['data' => $object], 200);
希望这对某人有所帮助。谢谢
我有两个模型 Roles
和 Specialisation
,它们之间的关系是 hasMany
和 belongsTo
。我有另一个名为 Company
的模型,它与每个模型都有 Many-to-many
关系。我正在制作一张图表,我需要有如下数据集:
data: {
Consultant: { "Avenger": 14, "Challenger": 647 },
Contractor: { "124 Spider": 4478, "500": 12685, "500L": 1664, "500X": 7665 },
Manufacturer: { "C-Max": 390, "Edge": 1423, "Escape": 308296, "E-Series": 53304, "Expedition": 5183, "Explorer": 2731, "Fiesta": 46249},
Miscellaneous: { "C-Max": 18390, "Edge": 142603, "Escape": 308296, "E-Series": 53304, "Expedition": 883, "Explorer": 2731 },
Owner: { "C-Max": 18390 },
Supplier: { "Expedition": 5883, "Explorer": 271131, "Fiesta": 46249, "Flex": 2289, "Focus": 1583 },
}
这里 Consultant
, Contractor
, Manufacturer
, ...等代表 role->name
, 在对象的索引里面我有 specialisation->name
和代表与他们相关的公司数量的数字,我想用以下代码实现:
$data = Role::whereNull('parent_id')->get();
$roles = collect($data)->map(function ($role) {
$specialisation = Specialisation::where('parent_id', $role->id)->get();
$element[$role->name] = array(
//specialisation and companies_count
);
return $element;
});
我对实现这个有点困惑。帮我解决这个问题。欢迎提出任何建议。谢谢。
我无法从 collection
方法中找到任何解决方案。为此,我尝试在 stdClass()
的帮助下创建一个对象,然后在对象内部创建另一个对象,然后返回整个数据。像这样:
$data = Role::whereNull('parent_id')->withCount('companies')->get();
$object = new \stdClass();
foreach($data as $key => $value)
{
$name = $value->name;
$specialisation = Specialisation::where('parent_id', $value->id)->withCount('companies')->get();
$ob = new \stdClass();
foreach ($specialisation as $sp => $spec)
{
$n = $spec->name;
$ob->$n = $spec->companies_count;
}
$object->$name = $ob;
}
return response()->json(['data' => $object], 200);
希望这对某人有所帮助。谢谢