【问题标题】:Laravel: How can I return an array within a collection?Laravel:如何在集合中返回数组?
【发布时间】:2017-02-06 12:08:09
【问题描述】:

我正在通过这样的 api 调用请求所有运动员数据,一切都很好。

MyController.php

public function get($year, $position_id)
{
    $collection = collect(
        $athletes = Athlete::where('athletes.graduation_year', $year)
            ->join('athlete_position', 'athlete_position.athlete_id', '=', 'athletes.id')
            ->where('athlete_position.position_id', '=', $position_id)
            ->join('evaluations', 'evaluations.athlete_id', 'athletes.id')
            ->whereNotNull('evaluations.comments')
            ->where('evaluations.status', 'published')
            ->orderBy('rank', 'asc')
            ->orderBy('rating', 'asc')
            ->orderBy('last_name', 'asc')
            ->get()
    )->groupBy('rating');

    return response()->json(['data' => $collection], 200);
}

我现在想将我的优惠表添加到此联接:

...
->join('evaluations', 'evaluations.athlete_id', 'athletes.craft_id')
->whereNotNull('evaluations.comments')
->where('evaluations.status', 'published')
->join('offers', 'offers.athlete_id', 'athletes.craft_id')
...

这行得通,但现在我得到了重复的运动员;每个报价一个。例如,如果某位运动员有 3 份报价,我将让同一名运动员返回 3 次 - 每个报价一次。

我想要的是集合中的一系列优惠。看起来像这样:

$athlete {
    ...
    'evaluation': '<p>My evaluation...</p>',
    'offers': [
        {'school': '<p>Clemson</p>','committed': 1},
        {'school': '<p>Alabama</p>', 'committed': 0} 
    ]
    ...
 }

这样我只能为每个运动员获得一个记录,但每个运动员可以有多个报价。

我的模型如下所示:

Athlete.php

public function offers()
{
    return $this->hasMany('App\Offer');
}

Offer.php

public function athletes()
{
    return $this->belongsToMany('App\Athlete');
}

所有数据都在我的应用程序中正确返回 - 只是 API 调用是我苦苦挣扎的地方。

感谢您的任何建议!

编辑

这是我的选择现在的样子:

$collection = collect(
        $athletes = DB::table('athletes')->select('craft_id', 'first_name', 'last_name', 'email', 'high_school_state', 'graduation_year', 'rank', 'rating', 'evaluations.comments', 'offers.school')
            ->where('athletes.graduation_year', $year)
            ->join('athlete_position', 'athlete_position.athlete_id', '=', 'athletes.craft_id')
            ->where('athlete_position.position_id', '=', $position_id)
            ->join('evaluations', 'evaluations.athlete_id', 'athletes.craft_id')
            ->whereNotNull('evaluations.comments')
            ->where('evaluations.status', 'published')
            ->join('offers', 'offers.athlete_id', 'athletes.craft_id')
            ->orderBy('rank', 'asc')
            ->orderBy('rating', 'asc')
            ->orderBy('last_name', 'asc')
            ->get()
    )->groupBy('rating');

它正在返回数据,如果我有多个报价,而不是将报价作为数组获取,我会返回两个完整的记录。

"data": {
    ...
  {
    "first_name": "Tyler",
    "last_name": "Durden",
    "offers":  "Clemson"
  },
   {
    "first_name": "Tyler",
    "last_name": "Durden",
    "offers":  "Alabama"
  },

我想得到:

"data": {
...
  {
    "first_name": "Tyler",
    "last_name": "Durden",
    "offers":  [
        "school": "Clemson"
        "school": "Alabama"
    ]
  },

【问题讨论】:

    标签: laravel-5.3


    【解决方案1】:

    这样复杂的查询最好用方法

    ->select('athletes.*') etc
    

    这样可以避免重复

    documentation

    【讨论】:

    • 我不认为我跟随。我更新了我的问题,尝试展示我所做的更改以尝试您的建议。
    猜你喜欢
    • 1970-01-01
    • 2020-01-05
    • 2015-04-30
    • 2016-06-21
    • 2019-04-03
    • 1970-01-01
    • 2018-12-07
    • 2016-07-20
    • 2020-10-14
    相关资源
    最近更新 更多