问题:如何根据“综述”栏目以及其他相关数据(用户,地址,城市,国家/地区,评论,活动)获得最佳3个组织
PS。组织有很多评论,列General是1-5级。
我还为每个表及其关系建立了模型: 示例表组织:
class Organization extends Model {
protected $table = 'organization';
protected $fillable = ['OrgName'];
public function user() {
return $this->belongsTo('App\Models\User', 'UserId', 'Id');
}
public function address() {
return $this->belongsTo('App\Models\Address', 'AddressId', 'Id');
}
public function review() {
return $this->hasMany('App\Models\Review', 'OrganizationId', 'Id');
}
public function activity() {
return $this->belongsTo('App\Models\Activity', 'ActivityId', 'Id');
}
}
到目前为止,我得到了这个:
class OrganizationRepository {
protected $organization;
function __construct(Organization $organization)
{
$this->organization = $organization;
}
public function best3() {
return $this->organization->with(['address.city.country', 'activity', 'review' => function($query) {
$query->orderBy('Timestamp', 'desc')->take(1);
}, 'review.user' ])->where()->take(3)->get();
}