0

I have the following query:

$data = DB::table('users')
->select(['id', 'firstName AS inputFirstName', 'lastName AS inputLastName', 'email AS inputEmail'])
->where('id', '=',$id )
->first();

It returns object but I need array and I convert it in this way:

$array_data =  json_decode(json_encode((array) $data), true);

But I'm not sure this is the right way to do it! I'm using Laravel 4.2 and I need to make this select query with aliases, that's why I'm not using Eloquent but this raw query.

Is there better way to make this select query to return array? Thanks!

2
  • 2
    Try casting the object to an array $array_data = (array)$data; Commented Apr 11, 2016 at 19:19
  • Thanks, it is array now! :) Commented Apr 11, 2016 at 19:23

2 Answers 2

1

You can use the toArray method.

You haven an object and you can transform it to and array.

$dataArray=$data->toArray();

You can find this method in the Laravel documentation.

Sign up to request clarification or add additional context in comments.

Comments

0

If you run into problems, this will give you a pure PHP solution.

foreach($tags as $object) {
  $tag_array[] = $object->name;
}

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.