12

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 ?

7 Answers 7

23

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.

8
  • Thanks @Alex I just fixed that with return $this->respond(Project::find($Id)->setAppends([]));; Commented Nov 8, 2017 at 14:40
  • I tried this in my model but didn't worked, Don't know why. prntscr.com/h7t9nt Commented Nov 8, 2017 at 14:42
  • as in, it still made the queries? Commented Nov 8, 2017 at 14:59
  • Yes. And it appends every keys also Commented Nov 8, 2017 at 15:00
  • I'm not sure it works as a scope, but really the second half of my comment is worth exploring. Commented Nov 8, 2017 at 15:08
18

It was usefull for me:

$services = Service::all()->makeHidden(['appendField1', 'appendField2'])->toArray();
0
15
// YourModel.php

public static $withoutAppends = false;

public function scopeWithoutAppends($query)
{
    self::$withoutAppends = true;

    return $query;
}

protected function getArrayableAppends()
{
    if (self::$withoutAppends){
        return [];
    }

    return parent::getArrayableAppends();
}
$result = YourModel::withoutAppends()->all();
2
  • works one caveat is it doesn't work for models called through relationships, just top level. Commented Jul 14, 2021 at 13:16
  • 1
    You should make sure to reset property $withoutAppends to false, otherwise any successive queries will be without the appends :|
    – SyncroIT
    Commented Jan 12 at 10:33
3

I had a problem with the append fields of the second model i was trying to access, it was going into an infinite loop. So in User Model;

Instead of

return $this->organisation->is_enabled;

using:

return $this->organisation->setAppends([])->is_enabled;

Solved the problem for me since setAppends([]) function is unappending the unnecessary relations

3

You can try this

User::where('user_id',$user_id)->get()->makeHidden(['full_name','start_end_date']);
1
  • 2
    this will anyway fire all queries to db, Question is more focused on lot of queries, not just making properties hidden. Commented Feb 18, 2021 at 6:38
2

I don't know if it works for collection containing multiple records. But If you need it for a single model without relations, you can just

$project->attributesToArray();

https://laravel.com/docs/8.x/eloquent-serialization#serializing-to-arrays

1

In your case,

->makeHidden(['usages']) 

option can help you. To ignore other relations, YouModel::setEagerLoads([]) cab help you.

Remember that, you have to use makeHidden as Statically. For example,

ScientistModel::where('name','Aziz Sancar')->first()->makeHidden(['appendsName']);

Not the answer you're looking for? Browse other questions tagged or ask your own question.