【问题标题】:Laravel model eloquentLaravel 模型雄辩
【发布时间】:2021-08-11 22:57:31
【问题描述】:

您好,我对 Laravel 中的模型 eloquent 有疑问。我该如何改变这一点

SELECT *, CAST(`end` AS int) FROM `Punishments` WHERE (`PunishmentType` = :ban OR `PunishmentType` = :ipBan OR `PunishmentType` = :tempBan OR `PunishmentType` = :tempIpBan) AND (`end` > :now OR `end` = -1) ORDER BY `id` DESC LIMIT :limit OFFSET :offset

到这里

protected Ban $ban;

public function __construct(Ban $ban)
{
    $this->ban = $ban;
}

public function getAllBans()
{
    return $this->ban
        ->where('PunishmentType', '=', 'BAN')
        ->where('PunishmentType', '=', 'IP_BAN')
        ->where('PunishmentType', '=', 'TEMP_BAN')
        ->where('PunishmentType', '=', 'TEMP_IP_BAN')
        ->where('end', '=', -1)
        ->orWhere('end', '>', time())
        ->orderBy('id', 'desc')
        ->paginate(20);
}

这不起作用:(

->where('end', '=', -1)
->orWhere('end', '>', time())

【问题讨论】:

  • 使用orWhere 作为惩罚类型? (PunishmentType 不能同时为'BAN''IP_BAN'
  • 是的,但我不知道如何用雄辩的语言做“或”
  • 你已经在使用->orWhere('end', '>', time())

标签: php sql laravel eloquent model


【解决方案1】:

可能类似于以下内容:

    return $this->ban
        ->whereIn('PunishmentType', ['BAN', 'IP_BAN', 'TEMP_BAN', 'TEMP_IP_BAN'])
        ->where(function($query) {
            $query->where('end', '>', time())
            orWhere('end', -1);
        })
        ->orderBy('id', 'desc')
        ->paginate(20);

【讨论】:

  • 仍会显示结束日期为两周前的项目。这可能与数据库中的时间戳为 13 位有关吗?
  • 是的。问题是“结束”中有 13 位数字。当我添加 time().'000' 时,问题不是
猜你喜欢
  • 2015-08-15
  • 1970-01-01
  • 2013-05-10
  • 2019-02-12
  • 2013-06-04
  • 2016-11-26
  • 1970-01-01
  • 1970-01-01
  • 2018-02-19
相关资源
最近更新 更多