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

rizaldywirawan's avatar

Laravel Eager Loading vs Database View

Hello,

I'm a newbie in Laravel, can someone explain to me, why people doesn't use database view nowadays, they use Laravel Eager Loading intstead.

A few of people said they do not use view.

Or, is there a way to achieve this with different way?

Thank you

0 likes
7 replies
rsvb's avatar

I think I was one of the last who used views lol. I stopped with the views when I started with Laravel, because you can now change your data retrieving in the php files, what is a little bit more handy than changing the views in the database.

rizaldywirawan's avatar

@rsvb Halo,

do you use quiry builder or eloquent?

what about relation to other table (let's say that the view get left join from 5 tables).

jlrdw's avatar

Eager loading is basically getting the data when needed, or from an article:

Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query.

Sounds good but it breaks easily if huge dataset, example:

Company A  // top foreach
-------company a payables 1  //inner foreach
-------company a payables 2  //inner foreach

Next loop around

Company B  // top foreach
-------company b payables 1  //inner foreach
-------company b payables 2  //inner foreach

All is well My laravel eloquent eager loading is working so nice.

Now where problem comes in:

Next loop

Company C  // top foreach
-------company c payables 1  //inner foreach
-------company c payables 2  //inner foreach
-------But here there are 550,000 more
-------For company c.

Just an example of how an inner foreach can have a huge amount of data.

So just me, I like eloquent for some things, but if a query starts getting more complex, or dealing with a huge amount of data I prefer normal PDO with getPdo().

Of course most forum users aren't going to have datasets that large, this was just an example.

Also remember active record (eloquent) is not a language, rather a shortcut that gets converted at runtime back to normal sql.

rizaldywirawan's avatar

Hi @bwrice,

Ahhh i see, that's my approach so far. But like @jlrdw, when the query starts getting more complex, it's become a nightmare.

My partner said he will use view, but I don't know why, I guess people no longer use it (or is it just me?).

If start with PDO, should i define it at controller? Will I need to always rewrite it in different controller?

jlrdw's avatar

I once used views. But now I just use controller and model. But a view is still fine. I only used a view where a set of records were fairly constant.

For example a customer lookup table.

  • A form is being filled
  • certain info was needed, contact, phone, etc
  • Hence a popup lookup
  • click customer id
  • that triggers a TR (table row) event
  • ajax code finds customer with id of 124
  • required fields filled in.

That's basically all I used a view for, I did add a search at top to narrow down results.

1 like

Please or to participate in this conversation.