【问题标题】:Transform in laravel collection在 laravel 集合中转换
【发布时间】:2018-06-16 20:09:44
【问题描述】:

我正在尝试在我的图形应用程序中通过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();

我想将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');

即使我进行转换,我也会得到相同的 null 结果,

任何想法表示赞赏。谢谢

编辑:

数据格式如下:

labels = ['2000-01','2032-33','2024-25','2005-06'];

【问题讨论】:

  • 我认为你必须对$item->financial_year进行json解码,然后你才能使用它的内部属性
  • @rkj 数据来自 eloquent 模型。我已经更新了问题。
  • 为此添加 dd 以检查输出 dd($fRA->map(function ($item){ return $item->financial_year['year']; })->all() );
  • 你有->with('financialYear')在急切加载,那么你怎么能得到financial_year
  • $labels = $fRA->pluck('financialYear.year');

标签: php laravel laravel-5.6 laravel-collection


【解决方案1】:

您应该能够执行以下操作:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-27
    • 2018-02-16
    • 2018-08-11
    • 2018-08-01
    • 1970-01-01
    • 2016-05-12
    • 2020-01-06
    • 1970-01-01
    相关资源
    最近更新 更多