kodeine / laravel-meta

Fluent Meta Data for Eloquent Models, as if it is a property on your model
MIT License
400 stars 90 forks source link

How to fetch models without any meta fields #105

Closed AlirezaSedghi closed 1 year ago

AlirezaSedghi commented 1 year ago

Hi there,

I'm trying to load data without fetching any meta fields along with it. I've attempted to use the without method, but it's not working as expected. Here's the code I tried:

$people = Person::without(['metas'])->get();

Can you suggest an alternative solution or help me figure out why this approach isn't working?

Thank you.

siamak2 commented 1 year ago

Hello Metas are not retrieved from database by default. They're only loaded if you either used Model::with('metas') or trying to access a meta.

AlirezaSedghi commented 1 year ago

But currently, with the eloquent query that I wrote, Laravel executed the following database queries to produce the result:

Debugbar

The picture above shows that 36500 MetaData models were loaded, using the following query for each one, even though I did not use "with('metas')": select * frompeople_metawhereperson_id= * andperson_idis not null

siamak2 commented 1 year ago

Send the code of your eloquent query builder and how you use it's result, so I can reproduce the issue. Does queries tab show the location and line where that query was performed?

AlirezaSedghi commented 1 year ago

Thanks to the helpful answers provided by @siamak2, I was able to identify the problem. The issue was caused by attempting to retrieve model attributes in the view. When attempting to retrieve a single attribute value of the model, all of its attributes were being fetched simultaneously.