在laravel中首先按关系排序

Posted

技术标签:

【中文标题】在laravel中首先按关系排序【英文标题】:Sort by relationship first in laravel 【发布时间】:2019-06-17 23:23:45 【问题描述】:

我有两个表:admins 和 log_doctor_infos。 admins 表有 hasOne 和 log_doctor_infos throught doctor_id 这样的关系。

在模型管理员中:

public function logDoctorInfo() 
    return $this->hasOne(LogDoctorInfo::class, 'doctor_id', 'id');
    // Model LogDoctorInfo is log_doctor_infos table

在模型 LogDoctorInfo 中:

public function doctor()
    return $this->belongsTo(Admin::class, 'doctor_id', 'id');
    // Model Admin is admins table

我得到所有数据表单管理员表,我想将与 log_doctor_infos 有关系的记录排序到顶部。

黄色记录,和log_doctor_infos有关系,我想排在最前面。

编辑:我在此查询中使用分页,我真的想获得黄色记录的数量。

感谢阅读!

在我的控制器中,我有自定义过滤器和分页。帮帮我。

public function index(Request $request) 
    $fullname = $request->query('fullname', NULL);
    $phone = $request->query('phone', NULL);
    $status = $request->query('status', NULL);

    $doctors = (new Doctor)->newQuery();
    if ($fullname != NULL) 
        $doctors = $doctors->where('fullname', 'LIKE', '%'.$fullname.'%');
    
    if ($phone != NULL) 
        $doctors = $doctors->where('phone', 'LIKE', '%'.$phone.'%');
    
    if ($status != NULL) 
        $doctors = $doctors->where('status', $status);
    
    $doctors = $doctors
    // ->with(array('logDoctorInfo' => function($query) 
    //     $query->orderBy('updated_at', 'ASC');
    // ))
    ->latest()
    ->paginate()
    ->appends([
        'fullname' => $fullname,
        'phone' => $phone,
        'status' => $status
    ]);
    // dd($doctors);
    return view('admin.doctors.index', compact('doctors'));

【问题讨论】:

【参考方案1】:

您可以使用withCount 方法。

Admin::withCount('logDoctorInfo')
       ->orderBy('log_doctor_info_count', 'desc')
       ->paginate(5);

您的控制器将如下所示

public function index(Request $request) 
    $fullname = $request->input('fullname', NULL);
    $phone = $request->input('phone', NULL);
    $status = $request->input('status', NULL);

    $doctorQuery = Doctor::query();
    if ($fullname) 
        $doctorQuery->where('fullname', 'LIKE', '%'.$fullname.'%');
    
    if ($phone) 
        $doctorQuery->where('phone', 'LIKE', '%'.$phone.'%');
    
    if ($status) 
        $doctorQuery->where('status', $status);
    
    $doctorQuery->withCount('logDoctorInfo')
        ->orderBy('log_doctor_info_count');

    $doctors = $doctorQuery->paginate()
        ->appends([
            'fullname' => $fullname,
            'phone' => $phone,
            'status' => $status
        ]);
    // dd($doctors);
    return view('admin.doctors.index', compact('doctors'));

【讨论】:

它有效,但我不知道如何将您的代码与分页和自定义过滤器一起使用,例如我的控制器。你能帮帮我吗? 你必须将 get() 方法替换为 paginate(5)。 这不是真正的工作。我没有你的代码中的 logDoctorInfo_count ->orderBy('logDoctorInfo_count', 'desc')。 Eloquent 会在您使用 withCount('relationName') 方法时创建该字段 relation_name_count。正如 Jinal 所说,只需将 get() 替换为 paginate()。我用一个例子更新了我的答案 你做$doctors->where('relation_name_count', '>', 0)->count()where()count() 是 Laravel 集合对象的有效方法。但这只会为您提供当前页面中的计数。如果您想要数据库中“黄色”元素的计数,则需要运行计数查询【参考方案2】:
Doctor::with('logDoctorInfo')->get()->sortByDesc('logDoctorInfo.id');

【讨论】:

它的工作。但是当我使用分页时,它不起作用。它说:“方法分页不存在。”帮帮我? 试试 - Doctor::with('logDoctorInfo')->paginate(10)->sortByDesc('logDoctorInfo.id'); 它的工作。但是分页无法工作。上面写着Method links does not exist. 在行 $doctors->links('admin.paginations.pagination_sm')

以上是关于在laravel中首先按关系排序的主要内容,如果未能解决你的问题,请参考以下文章

Laravel,按最新创建的子关系排序模型

按他们获得的点数排序评论,Laravel 5.2 多态关系

Laravel Eloquent 按关系计数排序

laravel 雄辩的按关系排序

Laravel Eloquent 按关系表列排序

Laravel Eloquent 模型按局部列和关系模型列值排序