enfix
enfix

Reputation: 6970

Laravel - Hide column in Scope

i added a relation in my scope:

public function apply(Builder $builder, Model $model)
{
    $builder->with('user');
}

how can I prevent all columns from being extracted? I tryed to set:

->with('user:col1,col2')

but the relationship seems to be no longer "Eager".

Upvotes: 0

Views: 217

Answers (1)

jfadich
jfadich

Reputation: 6348

You have to always include the ID with eager loaded relationships. So make sure the ID is included in the columns you're requesting. Without the ID it can't map the related model back to the original model so it has to re-request it the first time it's used instead of being able to be eager loaded.

public function apply(Builder $builder, Model $model)
{
    $builder->with('user:id,col1,col2');
}

Upvotes: 2

Related Questions