8

I have use in model .

$data = DB::select('select * from users');
return $data;

But in controller I have got like this.

Array
(
    [0] => stdClass Object
        (
            [Id] => 10
            [Name] => Sachin
            [Gender] => M
        )

    [1] => stdClass Object
        (
            [Id] => 12
            [Name] => Sourav
            [Gender] => M
        )
)

But I want Like this

Array
    (
        [0] => Array
            (
                [Id] => 10
                [Name] => Sachin
                [Gender] => M
            )

        [1] => Array
            (
                [Id] => 12
                [Name] => Sourav
                [Gender] => M
            )
    )

I already tried using get() and toArray() but its giving call to unknown member function. Anyone have an idea how to resolved this issue.

4 Answers 4

8

If you just need to get raw nested arrays of data, without using Eloquent:

$data = DB::connection()->getPdo()
    ->query("SELECT * FROM users")
    ->fetchAll(\PDO::FETCH_ASSOC);

This should be much more efficient than the current accepted answer.

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

3 Comments

Accepted answer will build models and on large datasets those approaches will crash. This answer is better because on large datasets you want to work with more raw data.
performance-wise this is the only correct answer. The other answers all convert into collections first and then back into arrays. I know the OP didn't ask for the best performing solution but if you're looking to get array data instead of Eloquent data I imagine it's probably for optimization reasons, and this is a great way to do it.
however this answer will not log the query
7

You just use the laravel standard function

public function testData(){
    $data = YOUR_MODEL_NAME::all()->toArray();
    echo "<pre>";print_r($data);
}

UPDATED:

public function testData(){
    $data =  DB::select('select * from users');
    $data = collect($data)->map(function($x){ return (array) $x; })->toArray(); 
    echo "<pre>";print_r($data);
}

Output will be :

UPDATED:

Array
(
    [0] => Array
        (
            [email] => [email protected]
            [member_code] => BMC0001ADMIN
        )

    [1] => Array
        (
            [email] => [email protected]
            [member_code] => BMC0003ADMIN
        )

    [2] => Array
        (
            [email] => [email protected]
            [member_code] => BMC0002ADMIN
        )
)

Array
(
    [0] => Array
        (
            [id] => 1
            [drewry_user_id] => 1
            [email] => [email protected]
            [name] => BMC  Administrator
            [mobile] => 
            [member_name] => BMC-Admin
            [member_code] => BMC0001ADMIN
            [shipper_size] => Large
            [fk_role_id] => 1
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-12 04:56:31
            [updated_by] => 
            [deleted_at] => 
        )

    [1] => Array
        (
            [id] => 6
            [drewry_user_id] => 3
            [email] => [email protected]
            [name] => BMC  Analyst
            [mobile] => 
            [member_name] => BMC-Analyst
            [member_code] => BMC0003ADMIN
            [shipper_size] => Large
            [fk_role_id] => 3
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-26 10:55:41
            [updated_by] => 
            [deleted_at] => 
        )

    [2] => Array
        (
            [id] => 9
            [drewry_user_id] => 2
            [email] => [email protected]
            [name] => BMC Product Manager
            [mobile] => 
            [member_name] => BMC-Product-Manager
            [member_code] => BMC0002ADMIN
            [shipper_size] => Large
            [fk_role_id] => 2
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-14 09:09:10
            [updated_by] => 
            [deleted_at] => 
        )

    [3] => Array
        (
            [id] => 19
            [drewry_user_id] => 4
            [email] => [email protected]
            [name] => User 1  
            [mobile] => 
            [member_name] => User-1
            [member_code] => BMC0004CUSTOMER1
            [shipper_size] => Large
            [fk_role_id] => 4
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-27 11:31:20
            [updated_by] => 
            [deleted_at] => 
        )

    [4] => Array
        (
            [id] => 20
            [drewry_user_id] => 5
            [email] => [email protected]
            [name] => User 2  
            [mobile] => 
            [member_name] => User-2
            [member_code] => BMC0004CUSTOMER2
            [shipper_size] => Large
            [fk_role_id] => 4
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-18 06:34:08
            [updated_by] => 
            [deleted_at] => 
        )

    [5] => Array
        (
            [id] => 21
            [drewry_user_id] => 6
            [email] => [email protected]
            [name] => User 3  
            [mobile] => 
            [member_name] => User-3
            [member_code] => BMC0004CUSTOMER3
            [shipper_size] => Large
            [fk_role_id] => 4
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-18 06:35:25
            [updated_by] => 
            [deleted_at] => 
        )

    [6] => Array
        (
            [id] => 22
            [drewry_user_id] => 7
            [email] => [email protected]
            [name] => User 4  
            [mobile] => 
            [member_name] => User-4
            [member_code] => BMC0004CUSTOMER4
            [shipper_size] => Large
            [fk_role_id] => 4
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-18 06:35:54
            [updated_by] => 
            [deleted_at] => 
        )

    [7] => Array
        (
            [id] => 23
            [drewry_user_id] => 8
            [email] => [email protected]
            [name] => User 5  
            [mobile] => 
            [member_name] => User-5
            [member_code] => BMC0004CUSTOMER5
            [shipper_size] => Large
            [fk_role_id] => 4
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-25 15:35:46
            [updated_by] => 
            [deleted_at] => 
        )
)

I hope it will work to you.

6 Comments

I know its works with eloquent but I could not use Eloquent in my situation.so please do answer if you know how to do with DB::select
So you want it without of Eloquent ?
You will pass raw query always ?
yes I have row query. like DB::select('my query')
Please check my updated answer, is that work for you?
|
2

toArray is a model method of Eloquent, so you need to a Eloquent model, try this:

 User::all()->toArray();

http://laravel.com/docs/eloquent#collections

2 Comments

This only returns an array of models. I guess what Raj wants is an array of array. toArray() only changes collection to array.
This converts the data to a collection and then back to an array. Huge performance loss!
0

After several trials of the solutions suggested, the problem why Laravel complains that the returned set is db:query (and thereby not eloquent collection - am new to these terms) is that the given line of code is incomplete:

$data =  DB::select('select * from users');
//should change to something like
$data =  DB::select('select * from users')->get();
//then to get pure array, just add the to-array method as:
$data =  DB::select('select * from users')->get()->toArray();
//if the above does not work, then as a last resort (from answer here) 
$data = collect($data)->map(function($x){ return (array) $x; })->toArray();

I hope this helps someone in same situation. Tested at Laravel 11.x.

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.