Steven Selolo
Steven Selolo

Reputation: 161

Laravel eloquent multi index search query

I'm currently faced with a problem. I'm trying to query results from the DB using Laravel based on an object containing multiple glass orders.

Here's what I'm sending via the frontend:

{
"order": [
        {
            "length": "204",
            "width": "80",
            "height": "5",
            "quantity": "2",
        },
        {
            "length": "216",
            "width": "80",
            "height": "5",
            "quantity": "3",
        }
    ]
}

Here's the code sample:

    $arry=array();
    // $countIDs=0;

    foreach($request->order as $key=>$insert) {
                   
        $length = $request->order[$key]['length'];
        $width = $request->order[$key]['width'];
        $quantity = $request->order[$key]['quantity']
        $glasses =  Glass::orderBy('id')->select('id', 'length', 'width', 'height', 'breakage' ,'sold');
    
        $glasses->where('length', '>=', $length)
                ->where('width', '>=', $width)
                ->where('height', $height)
                // ->where('id', '>', $countIDs) tried this but it doesn't work as expected
                ->where(function($query){
                    $query->where('breakage', 'no')
                    ->Where('sold', 'no');
                });
    
        // $countIDs+=$quantity;
    
        // Using this array for the frontend (displaying it on a table)
        array_push(
            $arry,
            [
                'available_glasses' => $glasses->limit($quantity)->get(),
            ]
        );  
    }

Here's the results:

ID  |  Glass Code  |   Length  |  Width   |  Height
-------------------------------------------------------

                GLASS ORDER 1

1       #gls01          220         100         5
2       #gls02          220         100         5

                GLASS ORDER 2

1       #gls01          220         100         5
2       #gls02          220         100         5
3       #gls03          220         100         5

But it returns duplicates (in the DB I have 10+ Glasses of the same dimensions). I'm expecting 'GLASS ORDER 2' to contain #gls03, #gls04, #gls05. I suspect that it has to do with querying the DB again from scratch.

Question(s): How can I ensure that 'GLASS ORDER 2' continues from where 'GLASS ORDER 1' left off? Is there a way to persist the search query?

I'm really confused and would appreciate your help. Thanks

Upvotes: 0

Views: 302

Answers (1)

Steven Selolo
Steven Selolo

Reputation: 161

According to newbie's comment and many others (Laracasts) here's what I did:

Created an array to hold the glass ids

$ids_to_exclude = array();

Added WhereNotIn() in the search query

$glasses->where('length', '>=', $length)
        ->where('width', '>=', $width)
        ->where('height', $height)
        ->whereNotIn('id', $ids_to_exclude) // new addition
        ->where(function($query){
            $query->where('breakage', 'no')
            ->Where('sold', 'no');
        });

And lastly, populated the $ids_to_exclude array

$glasses_ids = $glasses->limit($request->obj[$key]['quantity']);
for ($i=0; $i < $request->obj[$key]['quantity']; $i++) { 
    if (!in_array($glasses_ids->get()[$i]['id'], $ids_to_exclude)) {
        $ids_to_exclude[] = $glasses_ids->get()[$i]['id'];
   }
}

It no longer duplicates but provides me with the desired results.

Upvotes: 1

Related Questions