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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make relationship methods public [Feature Request] #2555

Open
salaback opened this issue Jul 8, 2023 · 5 comments · May be fixed by #2588
Open

Make relationship methods public [Feature Request] #2555

salaback opened this issue Jul 8, 2023 · 5 comments · May be fixed by #2588
Labels
relations Relations between documents

Comments

@salaback
Copy link

salaback commented Jul 8, 2023

Is your feature request related to a problem?

When trying to insert relationships into models via a service provider, it is not possible because the mongo specific methods are protected. Laravel has these methods public for this reason.

Describe the solution you'd like

Swap out protected for public methods

Describe alternatives you've considered

Other than forking the package I don't see a work around.
I could create a public method on the target model which then returns the protected on, however this feels rather hacky and creates requirements on a third party package.

Additional context

Inserting a relationship via the service provider is not the most well documented features, but it is usefully for being able to decouple packages and works as follows:

public function boot(): void
{
    Parent::resolveRelationUsing('children', function ($parent) {
        return $parent->hasMany(Child::class);
    });
}
@GromNaN
Copy link
Member

GromNaN commented Jul 8, 2023

What protected methods do you need to make public? EmbedsRelations has 2 protected methods: embedsMany and embedsOne. The methods in HybridRelations are public already.

@salaback
Copy link
Author

salaback commented Jul 8, 2023

Sorry that wasn't clear, yes it is embedsMany and embedsOne that I need public.

@divine
Copy link
Contributor

divine commented Jul 8, 2023

Embedded relation needs a complete rewrite after that it'll be made public possibly until that it won't be touched.

@GromNaN GromNaN added the relations Relations between documents label Aug 29, 2023
@GromNaN
Copy link
Member

GromNaN commented Aug 30, 2023

I opened #2588 to make this methods public.
For my culture, do you have any documentation on how to "insert relationships into models via a service provider" ?

@alaminfirdows
Copy link

I opened #2588 to make this methods public. For my culture, do you have any documentation on how to "insert relationships into models via a service provider" ?

Are you looking for this?

use App\Models\Order;
use App\Models\Customer;
 
Order::resolveRelationUsing('customer', function (Order $orderModel) {
    return $orderModel->belongsTo(Customer::class, 'customer_id');
});

Ref: https://laravel.com/docs/10.x/eloquent-relationships#dynamic-relationships

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relations Relations between documents
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants