Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve a result set from a raw query as an array instead of an object in Laravel 3

By default, Laravel's raw query methods return results as arrays of stdClass objects:

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [username] => admin
            [password] => admin123
            [email] => [email protected]
            [created_at] => 2012-12-06 18:57:19
            [updated_at] => 2012-12-06 00:00:00
        )

    [1] => stdClass Object
        (
            [id] => 2
            [username] => userna
            [password] => user
            [email] => [email protected]
            [created_at] => 2012-12-06 00:00:00
            [updated_at] => 2012-12-05 00:00:00
        )
)

The question is how to have Laravel return an array of Arrays instead:

   Array
    (
        [0] => Array
            (
                [id] => 1
                [username] => admin
                [password] => admin123
                [email] => [email protected]
                [created_at] => 2012-12-06 18:57:19
                [updated_at] => 2012-12-06 00:00:00
            )

        [1] => Array
            (
                [id] => 2
                [username] => userna
                [password] => user
                [email] => [email protected]
                [created_at] => 2012-12-06 00:00:00
                [updated_at] => 2012-12-05 00:00:00
            )
    )
like image 375
Ramesh Avatar asked Dec 06 '12 14:12

Ramesh


People also ask

How do I get the query builder to output its raw SQL query as a string?

DB::QueryLog() works only after you execute the query using $builder->get() . If you want to get the raw query before or without executing the query, you can use the $builder->toSql() method.

How can I see SQL queries in laravel?

The first method to get the query of an Eloquent call is by using the toSql() method. This method returns the query without running it – good if you don't want to alter data and only get the query – but this method doesn't show the whole query if your query is more complex or if there are sub-queries.

What is a raw query in SQL?

Raw SQL, sometimes also called native SQL, is the most basic, most low-level form of database interaction. You tell the database what to do in the language of the database. Most developers should know basics of SQL. This means how to CREATE tables and views, how to SELECT and JOIN data, how to UPDATE and DELETE data.

What does DB Select Return?

The SQL SELECT statement returns a result set of records, from one or more tables. A SELECT statement retrieves zero or more rows from one or more database tables or database views. In most applications, SELECT is the most commonly used data manipulation language (DML) command.


3 Answers

You may also get all the result always as array by changing

application/config/database.php

'fetch' => PDO::FETCH_CLASS, 

on line 31 to

'fetch' => PDO::FETCH_ASSOC, 
like image 96
Sinan Eldem Avatar answered Oct 04 '22 11:10

Sinan Eldem


Original answer for Laravel 3

Eloquent has a method to_array()

From docs:

The to_array method will automatically grab all of the attributes on your model, as well as any loaded relationships.

$user = User::find($id);  return Response::json($user->to_array()); 

or

return Response::eloquent($user); 

If you are using fluent, you can do as Sinan suggested and change the global configuration to return an associative array rather than objects.

Alternatively, you can convert an object to and from JSON to convert it to an array although the global option would be preferable in most cases. You can use the following in projects where you prefer objects normally but in certain edge cases need an array. This method will not play well with Eloquent, use the methods above in that case.

$users = DB::table('users')->where('name', '=', 'david')->get();  return array_map(function($val) {     return json_decode(json_encode($val), true) }, $users); 

Another option would be to temporarily change the runtime configuration

Config::set('database.fetch', PDO::FETCH_ASSOC); 

For Laravel ~4

In Laravel 4 onwards, all method names conform to PSR-2 standards.

$user = User::findOrFail($id);  return Response::json($user->toArray());  // In Laravel 5 onward the functions are preferred to facades. return response()->json($user->toArray()); 
like image 29
David Barker Avatar answered Oct 04 '22 10:10

David Barker


I don't know if laravel has in built function for returning results as array but if not you can use this snippet:

Where $data is your returned array of objects

$data = json_decode(json_encode((array) $data), true);
like image 20
Dale Avatar answered Oct 04 '22 12:10

Dale