mysql - Laravel 中的条件 `orderBy` 或 `DB::Raw` inside `orderBy`

标签 mysql laravel laravel-5 eloquent sql-order-by

我正在尝试以 asc 顺序显示从今天开始的特定接下来几天的约会列表。(例如:接下来的 5 天或 10 天)。仅从今天开始,接下来的 5 天或 10 天(注意:天数将是动态的,我的意思是客户将设置)。当今天日期结束时,该日期不应进入我提到的日子(从今天到接下来的5天)。我正在尝试使用 orderBy 方法,如下所示 -

$today = date('Y-m-d');
$appointments = Appointment::with('Customer')
         ->orderBy(DB::raw("('appointment_time', '>=', ${today})"), 'asc')->get();

return view("ManageAppointment", compact(['appointments']));

当然,我收到了以下错误 -

SQLSTATE[21000]: Cardinality violation: 1241 Operand should contain 1 column(s) (SQL: select * from appointments order by ('appointment_time', '>=', 2019-07-27) asc)

我应该怎么做才能得到预期的结果,有人可以帮助我吗?

最佳答案

尝试一下。

$tomorrow = Carbon::tomorrow(); //2019-07-28 00:00:00
$nextdate = Carbon::now()->addDays(5); //dynamic day(5days, 10days) //2019-08-01 00:00:00

$appointments = Appointment::with('Customer')
                      ->whereBetween('appointment_time', [$tomorrow,$nextdate])
                      ->orderBy('appointment_time','asc')->get();

关于mysql - Laravel 中的条件 `orderBy` 或 `DB::Raw` inside `orderBy`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57233048/

相关文章:

php - 将 JSON 数据从 Controller 发送到 Blade 中的 View : Laravel 5. 2

php - API 路由的自定义 findOrFail

laravel - 无法在位置 24 (6) 解析时间字符串 (2018-03-10 13 :08:09. -708656):意外字符

php - 如何从表单中的空字段中获取空值?

mysql - 需要将 1 行数据转置为 1 列但多行

mysql - ASP.NET MVC + MySql Membership Provider,用户无法登录

php - mysql_fetch_array() 期望参数 1 是资源,给定的 bool 值

php - 限制 Laravel 5 分页链接

php - 如何在 laravel 中使用 multi required_if?

php - 无法通过 Composer 安装 laravel 安装程序