1

I have a static method in my User model which counts something, i am using a raw statement:

static function countOrders($userId)
{
   $sql = "SOME CUSTOM-COMPLICATED QUERY";
   $result = \DB::select( \DB::raw( $sql ) );
   return $result; // <-- toArray() ? returns Exception!
}

Somewhere in my controller:

$orders = User::countOrders(*USER-ID*);

How can I get an array with records as arrays and not objects without modifying the global configuration for the FETCH method?

1 Answer 1

3

You don't need to run toArray().

When executing raw queries select returns array, it's stated in the docs:

The select method will always return an array of results.

static function countOrders($userId)
{
   $sql = "SOME CUSTOM-COMPLICATED QUERY";
   $result = \DB::select( \DB::raw( $sql ) );
   return $result;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Are you sure? I'm getting a Call to a member function get() on a non-object
Well I noticed tha you have another error, you must do \DB::table(''name_of_table")->select(\DB::raw( $sql ))->get();
can you show us your "SOME CUSTOM-COMPLICATED QUERY"
I've edited my answer, your code was right, you don't need to do a toArray(), since select already returns an array. Sorry as I have misunderstood your problem
It returns an array, but each entry in that array is a stdObject, that's my issue. I want each entry to be an array as well.
|

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.