2

I'm working with legacy code. The old report engine uses associative arrays, Laravel's query builder returns an array of objects.

I need to turn objects into arrays. I've tried using:

\DB::connection('tars-test') //->setFetchMode(PDO::FETCH_ASSOC)

but that gets me Class 'App\Http\Controllers\PDO' not found

It's been suggested to put ->all() at the end of the query but that throws error Call to a member function all() on array

The most efficient way would be to set the fetchmode at runtime, for the legacy function and just for the legacy function. How do I do it?

2 Answers 2

1

You can use 'toArray' method: https://laravel.com/docs/5.3/collections#method-toarray

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

1 Comment

Doesn't work. The return from the query builder is an array (of objects), so calling toArray on it just throws an error.
1

Laravel 5.3 and lower

You went the right way but as you can see laravel is trying to find PDO in App\Http\Controllers\PDO which probably means you forgot to add use Illuminate\Database\PDO;

Laravel 5.4 and up

Since laravel 5.4 this is not an option. But you still can set fetch mode globally: Laravel 5.4 - How to set PDO Fetch Mode?

Or if you still wish to change it just locally:

Return values only (no keys/associative array) in Laravel

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.