在 laravel 集合中转换
Transform in laravel collection
我正在尝试通过 Laravel collection
在我的图形应用程序中进行一些数据转换。我想创建 labels
和 datasets
我有这样的数据:
[
{
"id": 1,
"company_id": 1,
"year": 25,
"turnover": "3449",
"profit": "3201",
"turnover_range": 25,
"financial_year": {
"id": 25,
"year": "2024-25"
}
},
{
"id": 2,
"company_id": 1,
"year": 33,
"turnover": "5616",
"profit": "5905",
"turnover_range": 25,
"financial_year": {
"id": 33,
"year": "2032-33"
}
},
{
"id": 3,
"company_id": 1,
"year": 1,
"turnover": "4309",
"profit": "8563",
"turnover_range": 175,
"financial_year": {
"id": 1,
"year": "2000-01"
}
},
{
"id": 4,
"company_id": 1,
"year": 14,
"turnover": "5936",
"profit": "8605",
"turnover_range": 25,
"financial_year": {
"id": 14,
"year": "2013-14"
}
},
{
"id": 5,
"company_id": 1,
"year": 29,
"turnover": "7156",
"profit": "3844",
"turnover_range": 75,
"financial_year": {
"id": 29,
"year": "2028-29"
}
},
{
"id": 6,
"company_id": 1,
"year": 6,
"turnover": "5868",
"profit": "633",
"turnover_range": 75,
"financial_year": {
"id": 6,
"year": "2005-06"
}
},
{
"id": 7,
"company_id": 1,
"year": 25,
"turnover": "5809",
"profit": "6831",
"turnover_range": 575,
"financial_year": {
"id": 25,
"year": "2024-25"
}
},
{
"id": 8,
"company_id": 1,
"year": 12,
"turnover": "1",
"profit": "1976",
"turnover_range": 25,
"financial_year": {
"id": 12,
"year": "2011-12"
}
},
{
"id": 9,
"company_id": 1,
"year": 30,
"turnover": "680",
"profit": "1222",
"turnover_range": 25,
"financial_year": {
"id": 30,
"year": "2029-30"
}
},
{
"id": 10,
"company_id": 1,
"year": 26,
"turnover": "8197",
"profit": "3687",
"turnover_range": 25,
"financial_year": {
"id": 26,
"year": "2025-26"
}
}
]
来自 eloquent 我的模型集合:
$fRA = FinancialAndRisk::whereHas('company', function($q) use($request){
$q->where('slug', $request->slug);
})
->with('financialYear')
->get();
我想把 financial_year
里面的所有 year
作为标签,所以我试过:
$labels = $fRA->pluck('financial_year.year');
它显示为空。
我又试了一次
$labels= $fRA->map(function ($item){
$item->fin_year = $item->financial_year['year'];
return $item;
})->pluck('fin_year');
即使我进行了转换,我也得到了相同的空结果,
任何想法表示赞赏。谢谢
编辑:
数据格式如下:
labels = ['2000-01','2032-33','2024-25','2005-06'];
您应该可以执行以下操作:
$labels = $fRA->map(function ($item) {
return $item->financial_year->year;
})->unique();
显然,如果您想包含重复项(如果有的话),请删除 unique()
。
或者,如果您在 FinancialYear 模型中设置了 hasMany
关系,并且在此之后您不需要 $fRA
,您可以这样做:
$labels = FinancialYear::whereHas('FinancialAndRisk.company', function ($query) use($request) {
$query->where('slug', $request->slug);
})->pluck('year');
我正在尝试通过 Laravel collection
在我的图形应用程序中进行一些数据转换。我想创建 labels
和 datasets
我有这样的数据:
[
{
"id": 1,
"company_id": 1,
"year": 25,
"turnover": "3449",
"profit": "3201",
"turnover_range": 25,
"financial_year": {
"id": 25,
"year": "2024-25"
}
},
{
"id": 2,
"company_id": 1,
"year": 33,
"turnover": "5616",
"profit": "5905",
"turnover_range": 25,
"financial_year": {
"id": 33,
"year": "2032-33"
}
},
{
"id": 3,
"company_id": 1,
"year": 1,
"turnover": "4309",
"profit": "8563",
"turnover_range": 175,
"financial_year": {
"id": 1,
"year": "2000-01"
}
},
{
"id": 4,
"company_id": 1,
"year": 14,
"turnover": "5936",
"profit": "8605",
"turnover_range": 25,
"financial_year": {
"id": 14,
"year": "2013-14"
}
},
{
"id": 5,
"company_id": 1,
"year": 29,
"turnover": "7156",
"profit": "3844",
"turnover_range": 75,
"financial_year": {
"id": 29,
"year": "2028-29"
}
},
{
"id": 6,
"company_id": 1,
"year": 6,
"turnover": "5868",
"profit": "633",
"turnover_range": 75,
"financial_year": {
"id": 6,
"year": "2005-06"
}
},
{
"id": 7,
"company_id": 1,
"year": 25,
"turnover": "5809",
"profit": "6831",
"turnover_range": 575,
"financial_year": {
"id": 25,
"year": "2024-25"
}
},
{
"id": 8,
"company_id": 1,
"year": 12,
"turnover": "1",
"profit": "1976",
"turnover_range": 25,
"financial_year": {
"id": 12,
"year": "2011-12"
}
},
{
"id": 9,
"company_id": 1,
"year": 30,
"turnover": "680",
"profit": "1222",
"turnover_range": 25,
"financial_year": {
"id": 30,
"year": "2029-30"
}
},
{
"id": 10,
"company_id": 1,
"year": 26,
"turnover": "8197",
"profit": "3687",
"turnover_range": 25,
"financial_year": {
"id": 26,
"year": "2025-26"
}
}
]
来自 eloquent 我的模型集合:
$fRA = FinancialAndRisk::whereHas('company', function($q) use($request){
$q->where('slug', $request->slug);
})
->with('financialYear')
->get();
我想把 financial_year
里面的所有 year
作为标签,所以我试过:
$labels = $fRA->pluck('financial_year.year');
它显示为空。
我又试了一次
$labels= $fRA->map(function ($item){
$item->fin_year = $item->financial_year['year'];
return $item;
})->pluck('fin_year');
即使我进行了转换,我也得到了相同的空结果,
任何想法表示赞赏。谢谢
编辑:
数据格式如下:
labels = ['2000-01','2032-33','2024-25','2005-06'];
您应该可以执行以下操作:
$labels = $fRA->map(function ($item) {
return $item->financial_year->year;
})->unique();
显然,如果您想包含重复项(如果有的话),请删除 unique()
。
或者,如果您在 FinancialYear 模型中设置了 hasMany
关系,并且在此之后您不需要 $fRA
,您可以这样做:
$labels = FinancialYear::whereHas('FinancialAndRisk.company', function ($query) use($request) {
$query->where('slug', $request->slug);
})->pluck('year');