php - Convert Laravel collection to Array without out quotes before and after brackets

How to i remove the quotes before array opening bracket and after closing bracket.
private function get_sector_id ()
{
$sectors = SectorUser::select('sector_id')->where('user_id', auth()->user()->id)->pluck('sector_id');
dd(json_encode($sectors));
}
Output before json_encode:
Illuminate\Support\Collection {#310 в–ј
#items: array:2 [в–ј
0 => "49ea267e-d11d-4fe9-ba62-c16d620688a3"
1 => "6ba71f44-3c29-4fe3-b7ce-9d8a15e60f33"
]
}
tried to use json_encode but returns with quotes before and after array brackets
"["49ea267e-d11d-4fe9-ba62-c16d620688a3","6ba71f44-3c29-4fe3-b7ce-9d8a15e60f33"]"
i want to convert the output to brackets array so i can use in query whereNotIn,
target is:
["49ea267e-d11d-4fe9-ba62-c16d620688a3","6ba71f44-3c29-4fe3-b7ce-9d8a15e60f33"]
Answer
Solution:
This following code returns a Collection with elements being sector_ids.
$sectors = SectorUser::select('sector_id')->where('user_id', auth()->user()->id)->pluck('sector_id');
Luckily Laravels whereNotIn can take Collections or Arrays. Simply doing the following will work. As can be seen here.
$sectorsWithoutSome = Sectors::whereNotIn($sectors)->get();
I would argue, for other than responses, it is an anti pattern to convert your internal models to JSON with json_encode. There is often way better approaches.
Answer
Solution:
Use flatten on your collection.
$sectors->flatten();
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: 403 this action is unauthorized.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.