Rickin Rathatha
Rickin Rathatha

Reputation: 177

Laravel eloquent big difference in speed and amount of content returned

I have been testing out Laravel, I ran the following query:

 $site = Sites::where('url', '=', Request::server('HTTP_HOST'));

The database only contains 1 record and I var_dump $site and I get the output similar to below except is has nearly 900,000 characters returned.

If I keep the query simple, such as:

$site = Sites::all();

the output is:

object(Illuminate\Database\Eloquent\Collection)#121 (1) { ["items":protected]=> array(1) { [0]=> object(App\Sites)#122 (26) { ["connection":protected]=> string(5) "mysql" ["table":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(9) { ["id"]=> int(75) ["name"]=> string(4) "test" ["url"]=> string(7) "test.oo" ["site_type"]=> string(4) "CASH" ["active"]=> int(1) ["site_vat"]=> string(6) "20.000" ["theme"]=> string(4) "test" ["api"]=> string(8) "test.php" ["order_prepend"]=> string(4) "TEST" } ["original":protected]=> array(9) { ["id"]=> int(75) ["name"]=> string(4) "test" ["url"]=> string(7) "test.oo" ["site_type"]=> string(4) "CASH" ["active"]=> int(1) ["site_vat"]=> string(6) "20.000" ["theme"]=> string(4) "test" ["api"]=> string(8) "test.php" ["order_prepend"]=> string(4) "TEST" } ["changes":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } } } }

Which is considerably less (1,600 ish characters), the performance when running the first query is slow, am I doing something wrong as I am concerned about performance (why is the first query so slow).

Thanks

Upvotes: 0

Views: 2005

Answers (2)

YouneL
YouneL

Reputation: 8351

Use .get() method to retrieve results as collection object:

$site = Sites::where('url', '=', Request::server('HTTP_HOST'))->get()[0];

Or use .first() method to get the first model object that match your query:

$site = Sites::where('url', '=', Request::server('HTTP_HOST'))->first();

Upvotes: 0

milo526
milo526

Reputation: 5083

On the first instance you are returning a query builder instance where in the second instance you are actually returning the collection.

I.e. the first query will not return any actual data but a way to retrieve the data while the second query does fetch the data.

If you want the first query to return data and decrease the output size and time you would need to add a ->get() to the end of the query to actually execute the query.

Upvotes: 1

Related Questions