【问题标题】:Getting Odd or Even ID in Eloquent在 Eloquent 中获取奇数或偶数 ID
【发布时间】:2017-05-23 08:24:36
【问题描述】:

我有一个任务需要显示每种类型的假期,其中 id 是奇数。当我尝试在 Eloquent 中执行此操作时,它总是给我一个错误。

查询

Select holiday_type.id, holiday_type.name
From holiday_type
Where (holiday_type.id % 2) = 0;

Laravel PHP

return DB::table('holiday_type')
       ->select('holiday_type.id','holiday_type.name as text')
    // ->where(('id%2'), '=', 0)) ** I also tried different format but still nothing 
       ->get();
}

【问题讨论】:

标签: eloquent lumen


【解决方案1】:

您可以使用raw expression

DB::table('holiday_type')
    ->select(DB::raw('holiday_type.id, holiday_type.name as text'))
    ->whereRaw('MOD(id, 2) = 0')
    ->get();

【讨论】:

  • 嗨@Alfa 感谢您的回复。我试过你的,但它是空的。
  • 嗨@AlyssaAndrea,我已经更新了我的答案。请查看。
  • 嗨@Alfa 现在它的工作。谢谢你的帮助。祝你有美好的一天:))))))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-24
  • 1970-01-01
  • 2021-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多