Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

query laravel not working if pass variable in Where NOT IN

I’m trying to filter items from a database table
what I do is get the ids that I want to exclude and then through -> whereNotIn in laravel, I pass the ids

$idcontracts=array();
$idbike=array();
$biciCorretta = array();

$findcontract=Contract::whereRaw('? between data_inizio and data_fine', [$datainizio])->whereRaw('? between data_inizio and data_fine', [$datafine])->get();
        foreach ($findcontract as $key) {
            if (!in_array($key->id,$idcontracts)) {
                array_push($idcontracts,$key->id);
            }
        }
        
        foreach ($idcontracts as $idcontract) {
            $bike_contracts=DB::table('bike_contract')->where('contract_id',$idcontract)->get();
            foreach ($bike_contracts as $bike_contract) {
                if (!in_array($bike_contract->bike_id,$idbike)) {
                    array_push($idbike,$bike_contract->bike_id);
                }
            }
            
            
        }
        
        $notid=implode("', '",$idbike);

up to this point I have no problem.
the result of "implode" gives me the ids I want to remove

this is the result of $idbike and $notid:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

this is the query I write to exclude the ids found:

$bikes = Bike::with('category')->whereNotIn('id', [$notid])->orderBy('category_id')->get();

the problem is that it doesn’t exclude me the ids passed with $notid

but if I manually pass the ids, it removes them instead:

$bikes = Bike::with('category')->whereNotIn('id', [40,41,34,36,39])->orderBy('category_id')->get();

am I doing something wrong?

>Solution :

You shouldn’t implode $notid, that makes it a string and Laravels whereNotIn() already does that for you.

->whereNotIn('id', $idbike)

And remove the $notid parameter, as it is not needed.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading