Get data without appends and relations in Laravel from Eloquent

Issue

I have model Project which has some $appends and some relations like pages, brands, status etc.

And in $appends it has pages, and in Page model it has lot of other $appends. Now while I do Project::find($id) it retrieves project, it’s pages and other models which belongs to Page.
And I havn’t done with, so for each page it fires lots of queries.

Without using

DB::table('projects')->where('id', $id)->get();

Is there any better approach to load only Project model, no relation, no appends, nothing via Eloquent ?

Solution

Unfortunately thats not how it works. If you want to remove the appends you could do so as follows:

$project = Project::find(1);
$project->setAppends([]);

return $project; //no values will be appended

However if those appends are doing database calls and you prefer not to you should rexamine whether or not it makes sense for the appends to be there. It’s really a matter of whether you think it makes sense for the appends to always be included or not.

Answered By – Alex Harris

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published