Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8997295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:52:14+00:00 2026-06-15T23:52:14+00:00

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

  • 0

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] => admin@admin.com
            [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] => user@gmail.com
            [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] => admin@admin.com
                [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] => user@gmail.com
                [created_at] => 2012-12-06 00:00:00
                [updated_at] => 2012-12-05 00:00:00
            )
    )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:52:14+00:00Added an answer on June 15, 2026 at 11:52 pm

    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());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The default user registration page in Drupal 7 has fields to enter a username,
By default, the admin models are grouped by the app, and the app name
By default, phpMyAdmin shows me 30 rows whenever I load a table (the query
I'm trying to get my default template working with Laravel. I'm coming from Codeigniter
Default wp_list_authors will return authors list in the following format: <a href=#>AuthorName</a> (PostCount) I
Default django cache keys generator: def make_key(key, key_prefix, version): return ':'.join([key_prefix, str(version), key]) I
The default downloads folder in Windows 7 is c:\users\username\downloads for Chrome. But default downloads
The default downloads folder in Windows 7 is c:\users\username\downloads for Firefox. But default downloads
The default universe bounds are x=[-1 meter,1 meter], y=[-1,1] and z=[-1,1]. When objects intersect
Default JVM uses maximum 1.5 GB RAM/JVM Java application. But my Server have 8

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.