Support the ongoing development of Laravel.io →
posted 7 years ago

Hello everybody,

I'm currently in a bit of a pickle and I need to help where possible.

What I'm doing is accessing a GET route through AJAX to retrieve all products in my database as JSON. Getting the products through Eloquent as a collection takes about ~9 seconds, which isn't the fastest but it's acceptable. Now when I return this response as JSON or convert it in the json_encode() PHP-function, it start to pass my execution time of 30 seconds.

The way I send my data is as shown below:

$data = [
            'mysql'         => \App\Product::on('mysql')->get(),
            'source2'       => \App\Product::on('source2')->get(),
            'source3'       => \App\Product::on('source3')->get(),
            'source4'       => \App\Product::on('source4')->get(),
            'source5'       => \App\Product::on('source5')->get(),
        ];

return $data; // Equivalent to \Response::json($data);

Does anyone know why converting the collection into JSON takes so long?

Thank in advance.


EDIT/UPDATE:

I performed a test to see how long each part takes and the result is as follows:

Retrieving all products from all sources..
Result Eloquent non-encode: 8.315s
Now encoding the data into JSON
Encoding into json took: 91.645s

Well I took the time to write raw Queries to retrieve my (filtered) result and I reduced the time from ~100 seconds in total to ~0.20 seconds. I guess it's solved?

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.