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
JavaScript
x
$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:
this is the query I write to exclude the ids found:
JavaScript
$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:
JavaScript
$bikes = Bike::with('category')->whereNotIn('id', [40,41,34,36,39])->orderBy('category_id')->get();
am I doing something wrong?
Advertisement
Answer
You shouldn’t implode $notid
, that makes it a string
and Laravels
whereNotIn()
already does that for you.
JavaScript
->whereNotIn('id', $idbike)
And remove the $notid
parameter, as it is not needed.