【发布时间】:2020-07-10 11:23:04
【问题描述】:
我有一个评论表和 review_photos。我想通过其 updated_at 列从特定产品订单中获取所有评论,并优先考虑带有照片的评论,以便在特定日期内首先显示。下面的解决方案只会返回只有照片的评论,没有照片的评论不会显示。
审查模型
public function photos()
{
return $this->hasMany('App\Models\Booking\ReviewPhoto');
}
查看照片模型
public function review()
{
return $this->belongsTo('App\Models\Booking\Review');
}
评论库
public function getProductReviews($product_id, $paginate = false)
{
$query = $this->review->where('product_id', $product_id)->whereHas('photos')->orderByDesc('updated_at');
if ($paginate !== false) {
return $query->paginate($paginate);
}
return $query->get();
}
【问题讨论】:
-
你可以使用这样的东西。
$query = $this->review->where('product_id', $product_id)->orderByDesc('updated_at')->orderBy('photo_id');这将首先根据 updated_at 然后 photo_id 做短表,或者您可以将其更改为反之亦然。