在laravel集合中进行转换

时间:2018-06-16 08:42:15

标签: php laravel laravel-5.6 laravel-collection

我尝试在我的图表应用程序中通过Laravel collection进行一些数据转换。我想创建labelsdatasets

我的数据是这样的:

[
    {
        "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"
        }
    }
]

这是来自我的模型的雄辩收集:

$fRA =  FinancialAndRisk::whereHas('company', function($q) use($request){
    $q->where('slug', $request->slug);
})
    ->with('financialYear')
    ->get();

我想将year内的所有financial_year标记为标签,所以我尝试了:

$labels = $fRA->pluck('financial_year.year');

它显示为null。

我再次尝试

$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'];

1 个答案:

答案 0 :(得分:2)

你应该能够做到这样的事情:

$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');