Reputation: 637
I have the following array which I want to pass all values for "main_id" key to whereNotIn condition:
array:7 [
0 => array:1 [
"main_id" => "KWS1354767"
]
1 => array:1 [
"main_id" => "KWS1348470"
]
2 => array:1 [
"main_id" => "KWS1300790"
]
3 => array:1 [
"main_id" => "KWS1267286"
]
4 => array:1 [
"main_id" => "KWS1260614"
]
5 => array:1 [
"main_id" => "KWS1259115"
]
6 => array:1 [
"main_id" => "KWS1145684"
]
]
My code in controller is the following:
$search = $search->select("properties.prop_id as main_id")->get();
$searcharray = $search->toArray();
$prop_cnt = DB::table("offerdemandsmatchs")->whereNotIn('prop_id', $searcharray['main_id'])
->where('offerdemand_id', $o->id)
->get();
The error I get is:
ErrorException : Undefined index: main_id
If I manually pass the array it works:
$prop_cnt = DB::table("offerdemandsmatchs")->whereNotIn('prop_id', ['KWS1354767', 'KWS1348470', 'KWS1300790'])
->where('offerdemand_id', $o->id)
->get();
I can't get values from array and if I pass the values as in the first statement it fails. How can I pass array values as I'm doing it manually? Do I have to pass a collection?
Regards
Upvotes: 1
Views: 1019
Reputation: 15316
Use pluck instead of select and then convert into an array.
Pluck method will create an array for you. check method pluck
$prop_ids = $search->pluck('prop_id');
//$prop_ids = ['KWS1354767','KWS1348470'..];
$prop_cnt = DB::table("offerdemandsmatchs")->whereNotIn('prop_id', $prop_ids)
->where('offerdemand_id', $o->id)
->get();
Upvotes: 2
Reputation: 24276
It is not working because you pass the wrong array to the statement.
What you must do is to use array_column
on the search array:
DB::table("offerdemandsmatchs")
->whereNotIn('prop_id', array_column($searcharray, 'main_id'))
->where('offerdemand_id', $o->id)
->get();
Upvotes: 1