No One
No One

Reputation: 683

Laravel: How to use multiple where with Request

This code is working. It checks for the exact barcode, case sensitive:


    public function findpatronbarcode () {

        if ($findpatronbarcode = \Request::get('q')) {
            $patronbarcode = Patron::where(Patron::raw("BINARY `barcode`"), $findpatronbarcode)
                            ->where('debarred', NULL)
                            ->paginate(10);
        }else{
            $patronbarcode = ''; //If nothing found, don't return anything.
        }

        return $patronbarcode;
    }

However, I need to add one more WHERE clause to check if Expiration is greater than today. I tried this but it does not work.

    public function findpatronbarcode () {

        if ($findpatronbarcode = \Request::get('q')) {
            $patronbarcode = Patron::where(Patron::raw("BINARY `barcode`"), $findpatronbarcode)
                            ->where('debarred', NULL)
                            ->where('expiration','>',new Date())
                            ->paginate(10);
        }else{
            $patronbarcode = ''; //If nothing found, don't return anything.
        }

        return $patronbarcode;
    }

Upvotes: 1

Views: 46

Answers (2)

Motassem Kassab
Motassem Kassab

Reputation: 1815

This should work:

public function findpatronbarcode () {

    if ($findpatronbarcode = \Request::get('q')) {
        $patronbarcode = Patron::where(Patron::raw("BINARY `barcode`"), $findpatronbarcode)
                        ->where('debarred', NULL)
                        ->whereDate('expiration','>', date('Y-m-d'))
                        ->paginate(10);
    }else{
        $patronbarcode = ''; //If nothing found, don't return anything.
    }

    return $patronbarcode;
}

PS: as far as I understand, your question isn't about multiple "where" conditions in the request, as you've already achieved that in the working example, instead, it's about making a filter for date values.

Upvotes: 1

Kais Chrif
Kais Chrif

Reputation: 144

Check Laravel 7.x Where Clauses docs(still true for version 5.x and 6.x)

public function findpatronbarcode () {
    if ($findpatronbarcode = \Request::get('q')) {
        $whereClauses= [[Patron::raw("BINARY `barcode`"), '=', $findpatronbarcode], 
                       ['debarred','=', NULL], 
                       ['expiration','>', Carbon::now()]];
        $patronbarcode = Patron::where($whereClauses)->paginate(10);
    }else{
        $patronbarcode = ''; //If nothing found, don't return anything.
    }
    return $patronbarcode;
}

Upvotes: 0

Related Questions