Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add withOnly method #37144

Merged
merged 3 commits into from Apr 27, 2021
Merged

Conversation

JamesFreeman
Copy link
Contributor

I have a model, that has many $with and new relationships are added frequently. These relationships are used throughout 95% of the customer-facing websites. However, for certain sections of the Admin system, where only a few relationships need to be loaded.

Imagine you have a model like this:

class Product extends Model{
    protected $with = ['prices', 'colours', 'brand'];

    public function colours(){ ... }
    public function prices(){ ... }
    public function brand(){ ... }
}

At the moment, I would only be able to achieve this by doing:

Product::without(['prices', 'colours'])->get();

When adding future relationships to my base model, they would be included by default, it would be nice to be able to do something like:

I have added a new method called withOnly (I couldn't think of a better name - any suggestions?) - which will exclude all the base $with and only add the relations that you specify like the following:

Product::withOnly(['brand']);

If you are happy to merge this in, I'll send a PR to update the docs repo. 馃憤

Thanks,
James.

@taylorotwell taylorotwell merged commit acebcfc into laravel:8.x Apr 27, 2021
@JamesFreeman JamesFreeman deleted the feature/with-only branch April 27, 2021 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants