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?

gedq
  • 609
  • 9
  • 20

2 Answers2

1

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

Paul Androschuk
  • 796
  • 7
  • 9
  • Doesn't work. The return from the query builder is an array (of objects), so calling toArray on it just throws an error. – gedq Nov 17 '16 at 13:55
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