ivva
ivva

Reputation: 2949

How to convert object as array Laravel 4.2

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!

Upvotes: 0

Views: 1080

Answers (2)

TheRealJAG
TheRealJAG

Reputation: 2088

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

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

Upvotes: 0

Idir Ouhab Meskine
Idir Ouhab Meskine

Reputation: 587

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.

Upvotes: 1

Related Questions