Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

Method on Eloquent that reduces the size

I remember seeing a method on Eloquent that when being used, then you can see it reduces the loaded size on the browser. I can't remember which method it was, and it's not in the docs

0 likes
29 replies
Sinnbeck's avatar

The size of what? Fields gotten from the database? Number of rows? Converting eloquent models to stdClass?

1 like
Ligonsker's avatar

@Sinnbeck I don't remember :/ I just remember that I didn't find that in the regular docs then saw it in the API docs without explanation and then there was a vide on YouTube explaining what it is. Not sure how it does that

Snapey's avatar

@Ligonsker But what problem are you trying to solve?

The browser only receives the data that you echo in the blade view......

1 like
Sinnbeck's avatar

@Ligonsker Pretty sure there is no such thing in the docs as that. I know most of eloquents features, but guessing which one is close to impossible without any context. Api resources perhaps?

1 like
Ligonsker's avatar

@snapey @sinnbeck I am passing the result of an eloquent query to blade and I wanted to try to reduce the size of it. Not api resources, It was an actual method you use on the eloquent model

Sinnbeck's avatar

@Ligonsker You arent sending the eloquent data. You are sending html. So send less html

1 like
Ligonsker's avatar

@Sinnbeck So maybe this is what this method was doing? sending only the data and not the entire model data? It did something like that, not sure exactly what though Still haven't found

Sinnbeck's avatar

@Ligonsker In a blade view that would make no difference in the data sent to the client. Only how much memory in php and mysql. The client gets html, not php. So the only way to reduce it is to reduce the html output in blade

1 like
Ligonsker's avatar

@Sinnbeck oh now I get what you were saying earlier. I just remember seeing in the video that it reduced the transferred MB(when opening network tab in devtools). I was sure that if you pass an object to blade then even if you don't render it, it still transfers the data to the client. I must've been wrong then

Snapey's avatar

@Ligonsker perhaps you are thinking about zipping the content

I'll ask again

But what problem are you trying to solve?

1 like
Tray2's avatar

@Ligonsker Do you mean that instead of passing the complete record you just pass the fields that you need?

If so just add a ->select() before the call to get.

Example, you have a table with these columns

  • id
  • title
  • published
  • blurb
  • created_at
  • updated_at

And you only need to show the id, title and the published.

You can do this.

$books = Book::select('id', 'title', 'published')->get();

Instead of the regular

$books = Book::all();

You can of course use that with pagination as well, and you should do that.

1 like
Snapey's avatar

@Tray2 doesn't affect what is sent to the browser (unless it is sent as json, but we don't have that info)

1 like
Tray2's avatar

@Snapey True, but it sends less than it would if the whole record was sent.

1 like
Sinnbeck's avatar

@Ligonsker this was in my first question as well.

Converting eloquent models to stdClass?

But it does not send less data to the client

1 like
Ligonsker's avatar

@Sinnbeck Oh, so I guess I did not know that it does that.

So toBase converts eloquent to stdClass? I had no idea, I just remembered it vaguely

Ligonsker's avatar

@Sinnbeck So it returns purely the data without the extra eloquent details? Just the content of the data array? (Although I noticed eloquent returns 2 seemingly identical data arrays: original and attributes ?).

Originally it was an object array before converted to stdClass?

Sinnbeck's avatar

@Ligonsker what it actually does to be 100% specific is to convert it to a plain query builder. Like DB::table()...

1 like
Snapey's avatar

@Ligonsker

Although I noticed eloquent returns 2 seemingly identical data arrays:

An eloquent model object contains two arrays plus other attributes, but the point about your comment I have an issue with is it does not "return" this. It returns what you ask it for.

If you ask it to dump its entire contents then yes its a lot of data, but that data is just memory in your php script.

It does not send this to the browser.

It might get serialised and added to a queue or a cache

Thats the downside of all the extra functionality you get from an eloquent model.

so, I'll ask again, again

But what problem are you trying to solve?

1 like
Ligonsker's avatar

@Snapey I am trying to solve some slow loading time and plenty of "MB" loaded (as seen in the devtools).

But now I think I understood what you say - Even if I have the $some_eloquent variable in the xxx.blade.php file, but I don't output it, it will not cause the browser to have that "MB" transferred unless I actually display it.

So let's say the $some_eloquent contain 10MB of data and it's in the blade file, unless I actually output it somehow:

{{ $some_eloquent->some_column }}, it will not be downloaded by the browser

Please or to participate in this conversation.