2

I have code:

return $getId = Permission::select('id')->get();

This code returned me:

[{"id":1},{"id":2},{"id":3},{"id":4}]

I want to query returned the records in the form of:

["1","2","3","4"]

How can I do this? I tried to use json_decode, but it's not work I also tried to use a foreach loop, but the result was the same. I'm doing this because I want to compare the two tables using array_diff, but still I have error:

array_diff(): Argument #1 is not an array

Argument 1 is variable $getId.

2 Answers 2

4

You want to use pluck() and toArray() methods:

return $getId = Permission::pluck('id')->toArray();
Sign up to request clarification or add additional context in comments.

1 Comment

your answer is better :)
2

You can wrap the result in Collection and use pluck method:

$permissions = Permission::select('id')->get();
$ids = collect($permissions)->pluck('id')->all(); // all() to array

More on Laravel collections pluck here

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.