本文介绍了带有列值的 Laravel orderBy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我目前拥有的代码:

This is the code that I currently have:

$programmefundings = Programmefunding::where('status', '!=', 'draft')->orderByRaw("FIELD(status , 'open', 'announced', 'delayed', 'closed') ASC")->orderBy('date_start', 'desc')->get();

因此它获取我的所有计划资金,除了状态为 'draft' 的资金,将它们从 'open' 重新排序为 'closed' 并在 'date_start' 之前安排它们.

So it gets all of my programmefundings except the ones with the status 'draft', reorders them from 'open' to 'closed' and arranges them by 'date_start'.

我需要以'asc'的顺序对另一列'announcement_date'中状态为'announced'的项目资金进行重新排序,而不影响'open''关闭' 结构,它当前具有并且不影响没有状态值 'announced' 的任何其他程序发现.

I need to reorder the programmefundings which have the status 'announced' by another column 'announcement_date' in 'asc' order without disturbing the 'open' to 'closed' structure it currently has and without effecting any other programmefindings which do not have the status value 'announced'.

这可能吗?有人知道怎么做吗?

Is this possible and does anyone know how to do this?

谢谢!

推荐答案

您应该能够使用 IF 语句来做到这一点.

You should be able to do this with an IF statement.

$programmefundings = Programmefunding::where('status', '!=', 'draft')
    ->orderByRaw("FIELD(status , 'open', 'announced', 'delayed', 'closed') ASC")
    ->orderByRaw("IF(status = 'announced', accouncement_date, date_start) DESC")
    ->get();

这篇关于带有列值的 Laravel orderBy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:46