如何将orderBy()
与with()
关系一起使用orderBy
需要使用with()
引入的列。
已更新
return $rank = BusinessDetails::with(['rar' => function ($query) {
$query->orderBy('int_total_ratings_value', 'DESC');
}])
->select('pk_int_business_id', 'vchr_business_name',
'vchr_business_description', 'fk_int_category_id', 'fk_int_location_id')
->get();
商业模式
<?php
namespace App\BackendModel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\User;
use App\BackendModel\BusinessImages;
use App\BackendModel\Categories;
class BusinessDetails extends Model
{
use SoftDeletes;
const ACTIVATE = 1;
const DEACTIVATE = 0;
public static $days=[
['name'=>'Monday','id'=>1],
['name'=>'Tuesday','id'=>2],
['name'=>'Wednesday','id'=>3],
['name'=>'Thursady','id'=>4],
['name'=>'Friday','id'=>5],
['name'=>'Saturday','id'=>6],
['name'=>'Sunday','id'=>7]
];
protected $dates = ['deleted_at'];
protected $primaryKey = 'pk_int_business_id';
protected $table = 'tbl_business_details';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'vchr_business_name', 'vchr_business_description', 'vchr_telephone_number', 'vchr_company_mobile_number', 'vchr_company_email', 'vchr_contact_person', 'vchr_logo_path', 'fk_int_category_id', 'date_date_of_establishment', 'vchr_website', 'time_open_hours', 'time_close_hours', 'time_lunch_hours','int_closed_on','int_status', 'int_review_status','fk_int_user_id','fk_int_location_id','vchr_map','vchr_holiday'
];
public function rar()
{
return $this->hasOne('App\BackendModel\OverallRating', 'fk_int_business_id', 'pk_int_business_id')
->select('fk_int_business_id', 'float_overall_ratings' , 'int_no_of_ratings', 'int_total_ratings_value', 'int_no_of_reviews', 'int_no_of_like');
}
}
答案 0 :(得分:1)
您可以将关闭传递给with()
并在那里应用订单。
这是一个例子
Model::with(['relation' => function($query){
$query->orderBy('column', 'ASC');
}]);
更新
return $rank = BusinessDetails::with(['rar' => function ($query) {
$query->orderBy('rar.int_total_ratings_value', 'DESC');
}])
->select('pk_int_business_id', 'vchr_business_name',
'vchr_business_description', 'fk_int_category_id', 'fk_int_location_id')
->get();
希望这有帮助。