laravel leftjoin从第一列返回重复值(mysql)

sczxawaw  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(280)

列工作人员:

专栏推荐:

我的问题:

$getRecommendationWorker = Worker::select('workers.id', 'workers.name', 'workers.photo', 'workers.description', 'workers.profile_worker', 'recommendations.recommendation', 'recommendations.author', 'recommendations.work_author', 'recommendations.profile_author')
->leftJoin('recommendations', 'workers.id', '=', 'recommendations.worker_id')
->get();

laravel返回:

{"id":5,"name":"Piotr Debowski","photo":"3c29641e2a.jpeg","description":"sdasdasd","profile_worker":"https:\/\/codepen.io\/","recommendation":"pierwsza rekomendacja","author":"Jan Kowalski","work_author":"IT spec","profile_author":"https:\/\/stackoverflow.com\/questions\/43136250\/eloquent-join-table-with-two-conditions"}
{"id":5,"name":"Piotr Debowski","photo":"3c29641e2a.jpeg","description":"sdasdasd","profile_worker":"https:\/\/codepen.io\/","recommendation":"druga rekomendacja","author":"Jan Wo\u017aniak","work_author":"dsad","profile_author":"https:\/\/nczas.com\/2018\/08\/21\/wreszcie-zatrzymali-t"}

我有 id , name 以及表中的其他值 workers 两次回来。我想让你看起来像这样:

{"id":5,"name":"Piotr Debowski","photo":"3c29641e2a.jpeg","description":"sdasdasd","profile_worker":"https:\/\/codepen.io\/","recommendation":"pierwsza rekomendacja","author":"Jan Kowalski","work_author":"IT spec","profile_author":"https:\/\/stackoverflow.com\/questions\/43136250\/eloquent-join-table-with-two-conditions","recommendation":"druga rekomendacja","author":"Jan Wo\u017aniak","work_author":"dsad","profile_author":"https:\/\/nczas.com\/2018\/08\/21\/wreszcie-zatrzymali-t"}

我要从table上拿 workers 仅返回表中的一行和 recommendations 与第一个表相关的所有记录。当我使用 groupBy (例如: ...->->groupBy('workers.id') )只返回表中的第一条记录 recommendations . 我不能用 where ,因为所有值都将显示在同一页上。列 worker_id 与表相关 workers 编辑:
工人模型:

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

推荐模式:

public function workers()
{
    return $this->belongsTo('App\Worker');
}
yshpjwxd

yshpjwxd1#

最简单的方法是加载 recommendations 关系:

$workers = Worker::with('recommendations')->get();

然后通过循环访问这些建议。

@foreach($workers as $worker)

    @foreach($worker->recommendations as $recommendation)

        //

    @endforeach

@endforeach

但是,如果希望将行格式化为问题中的示例,则可以利用它们是集合的事实:

$workers = Worker::with('recommendations')->get()
    ->flatMap(function ($worker) {

        $recommendations = $worker->recommendations->isEmpty()
            ? collect([new \App\Recommendation])
            : $worker->recommendations;

        return $recommendations->map(function ($recommendation) use ($worker) {
            return (object)[
                'id'             => $worker->id,
                'name'           => $worker->name,
                'photo'          => $worker->photo,
                'description'    => $worker->description,
                'profile_worker' => $worker->profile_worker,
                'recommendation' => $recommendation->recommendation,
                'author'         => $recommendation->author,
                'work_author'    => $recommendation->work_author,
                'profile_author' => $recommendation->profile_author,
            ];
        });
    });

相关问题