0

I want to merge example_1 and example_2 values into array.

example_1 and example_2 are of type int.

User::select('example_1','example_2')->where('id',Auth::user()->id)->get();
// The result: [{"example_1":"1","example_2":"2"}]

example_1 and example_2 if has value 1 and 2 respectively.

I want to have an array : [1,2]

2 Answers 2

2

You can do one of this

$result = array_only(auth()->user()->toArray(), ['example_1','example_2']);

// Or

$result = User::where('id', auth()->id())->first(['example_1','example_2'])->toArray();

// Finally

$data = array_values($result);
Sign up to request clarification or add additional context in comments.

Comments

0

All Eloquent queries return Collection objects. Those can be modified easier (through collections methods).

You can use first() to get the first object out of this collection.

Now you can access the variables of the Eloquent User model by calling them like you'd call public class properties.


Your final code would look like:

$user = User::select('example_1','example_2')
          ->where('id',Auth::user()->id)->get()->first();

$array = [ $user->example_1, $user->example_2 ]; 
// The result: [1,2]

3 Comments

this is what I get : [{"a":"5","b":"4"}] I need only [5,4]
Oh, you are right. Give me a minute @MurlidharFichadia
@MurlidharFichadia Updated it :)

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.