Gooldzik
Gooldzik

Reputation: 13

Laravel model eloquent

Hello I have a problem with model eloquent in Laravel. How I can change this

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

To this

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);
}

This doesn't work :(

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

Upvotes: 0

Views: 32

Answers (1)

Repox
Repox

Reputation: 15476

Maybe something like the following:

    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);

Upvotes: 1

Related Questions