S
S
Sergey Popov2018-06-02 19:07:06
Laravel
Sergey Popov, 2018-06-02 19:07:06

Infer nested relation via one method in Laravel Eloquent Model?

Dear participants of the toaster, we really need help. I will be grateful for any answer.
In general, everything is very simple: there is a user model. Each user is associated with a card. This card has a subscription. Promotions are attached to the subscription, discounts are attached to the promotions.
user->cards->subscription->promotions->
discounts user()->discounts)? Or how it can be optimized.
P.S. The discount cannot be tied to a specific user due to the specifics of the business logic.
Eager Loading I know, but this is a wild chain of passes through arrays in the code. And the method must be used regularly.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Semenko, 2018-06-02
@be_a_dancer

There are at least two solutions:
1. Write a query yourself, then get a collection of models using the method Illuminate\Database\Eloquent\Builder::hydrate()+ repository for convenience.
2. Implement your own connection, since in laravel this is easy: it is enough to inherit the class Illuminate\Database\Eloquent\Relations\Relationand implement all the methods. For example, you can use standard links.
PS There is no way to solve this problem by standard means.

A
Artem, 2018-06-02
@proudmore

I don’t know how eloquent works in detail, but as an option, you can make a method in which you get a list of discounts and shove them among users. Those. User::fetchDismounts(array $users)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question