The size of what? Fields gotten from the database? Number of rows? Converting eloquent models to stdClass?
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
@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
@Ligonsker But what problem are you trying to solve?
The browser only receives the data that you echo in the blade view......
@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?
@Ligonsker You arent sending the eloquent data. You are sending html. So send less html
@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
@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
@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
@Ligonsker only in ajax request (which is a json version of it)
@Ligonsker perhaps you are thinking about zipping the content
I'll ask again
But what problem are you trying to solve?
@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.
@Tray2 doesn't affect what is sent to the browser (unless it is sent as json, but we don't have that info)
@Snapey True, but it sends less than it would if the whole record was sent.
@Tray2 maybe OP means paginate then.
@jlrdw Could be.
Do you mean toBase()?
@MichalOravec @sinnbeck @snapey @tray2 @jlrdw
Yes! @michaloravec found it 😀 this is the method I was talking about !
( and this is the video that I was talking about by the way: https://www.youtube.com/watch?v=sE4E5j32VoE )
Now I wonder if it really does what I was talking about
@Ligonsker So mark this thread as solved.
@Ligonsker this was in my first question as well.
Converting eloquent models to stdClass?
But it does not send less data to the client
@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 yup. Well it skips all accessors, casts etc and then returns stdClass
@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?
@Ligonsker what it actually does to be 100% specific is to convert it to a plain query builder. Like DB::table()...
@Sinnbeck Got it :D, thank you!
@Ligonsker happy to help
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?
@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
@Ligonsker Correct.
Please or to participate in this conversation.