代码之家  ›  专栏  ›  技术社区  ›  Nitish Kumar

Laravel系列中的转换

  •  0
  • Nitish Kumar  · 技术社区  · 7 年前

    我正在尝试通过 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"
            }
        }
    ]
    

    这是从我的模型的雄辩收集:

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

    显示为空。

    我又试过了

    $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 回复  |  直到 7 年前
        1
  •  2
  •   Rwd    7 年前

    $labels = $fRA->map(function ($item) {
        return $item->financial_year->year;
    })->unique();
    

    显然,移除 unique()

    hasMany $fRA 在这之后,你可以做:

    $labels = FinancialYear::whereHas('FinancialAndRisk.company', function ($query) use($request) {
        $query->where('slug', $request->slug);
    })->pluck('year');