简体   繁体   中英

How to use one-to-many relationship on controller. Laravel LaraPlans

Im using this package: https://github.com/gerardojbaez/laraplans and the documentation is a little confusing.

The information for the user can be taken from the suscription plan, but not viceversa so I'm having problems when trying to use 2 types (or more) of suscriptions plans.

I have this on my HomeController:

public function account()
{
    $user = Auth::user();

    $plan = $user->subscription('Pro');

    return view('auth.profile')->with('user', $user)->with('plan', $plan);
}

And this on my view

<li class="list-group-item">
    {{ $plan->ends_at }}
</li>
<li class="list-group-item">
    {{ $plan->user->name }}
</li>

From this you can have an idea of the way the Model is connected, now, as you can see on the HomeController Account function the way I'm accessing the suscription is by its name.

How can I know the active suscription for the Authtenticated User? I'm thinking that by the user_id on the plan_suscriptions table on the DB, but when trying to access it I always get NULL.

I think my aproach in logic is flawled.

In the PlanSubscriber Trait that comes on the package theres a function that seems to solve my problem:

/**
 * Get user plan subscription.
 *
 * @return \Illuminate\Database\Eloquent\Relations\HasOne
 */
public function subscriptions()
{
    return $this->hasMany(config('laraplans.models.plan_subscription'));
}

But when using a dd(); on my controller while using the function I get the following:

Function:

public function account()
{
    $user = Auth::user();

    $plan = $user->subscriptions();

    dd($plan);
}

DD:

在此处输入图片说明

As you can see, there are no attributes to get info from so I'm not really sure I'm using this relationship correctly.

That's the magic of Laravel.

You get the user model by calling Auth::user() so there you already have the relevant Model that you're looking for. That of the authenticated user.

Now inside the Model-Definition you likely have a method subscription which returns the Related Model. (You probably added some logic for the 'Pro' parameter).

Laravel here is developed with convention-over-configuration in mind and knows which rows in the table actually to look for. So what you need is a 'id' in each table, and due to the relationship a 'user_id' in the table holding the subscriptions. There is a way to define that differently in case. Here is the doc: https://laravel.com/docs/5.4/eloquent-relationships

So you got the plan from the relevant user, I don't see anything bad here.

===

You use $user->subscription but do an ->subscriptions() instead (notice the plural 's'). Use a loop (or ->first()) since you get plans and not one plan

Right now you always pass 'Pro' in but the authenticated user might now be subscribed to the Pro-Plan and hence returns null. Subscriptions returns the hasMany relation. From there /not tried/ you can loop over the result and get the names of the plan or whatever you need. Notice: It's a hasMany, although the user might just have subscribed to one subscription. So you need to loop or get the first result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM