列工作人员:
专栏推荐:
我的问题:
$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');
}
1条答案
按热度按时间yshpjwxd1#
最简单的方法是加载
recommendations
关系:然后通过循环访问这些建议。
但是,如果希望将行格式化为问题中的示例,则可以利用它们是集合的事实: