0

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:

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?

2
  • 1
    try ->whereNotIn('id', $idbike) .here implode is not neccessary since wherenotin expect array Commented Apr 5, 2022 at 8:32
  • my very trivial mistake, just read the laravel whereNotIn documentation, thanks a lot for the help now it works Commented Apr 5, 2022 at 8:41

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

implode will return in string, and because of that it will not work correctly, you should pass it as array instead.

Comments

1

If you print data

$idbike =[40,41,34,36,39];

print_r($idbike);

Output will be Array

Array
(
    [0] => 40
    [1] => 41
    [2] => 34
    [3] => 36
    [4] => 39
)

and if you print below code

$notid=[implode(",",$idbike)];
print_r($notid);

The output will be

Array
(
    [0] => 40,41,34,36,39
)

So your query become

->whereNotIn('id', ["40,41,34,36,39"])

so laravel searching for id of "40,41,34,36,39". so its not returning result

So you can pass array directly to wherenotin

->whereNotIn('id', $idbike)

Comments

1

Laravel, whereNotIn method removes elements from the collection that have a specified item value that is contained within the given array. That means you have to pass an array

So, idbike is the array you mentioned

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.