Lars-Janssen's avatar

A more Laravel way

Hello,

What would be more the laravel way of doing this?:

/**
 * Subscribe to a forum
 *
 * @param Forum $forum
 * @return bool
 */
public function subscribe(Forum $forum, $user)
{
    $subscription = new Subscription;
    $subscription->user_id = $user->id;
    $forum->subscriptions()->save($subscription);
}

The relation between forum and subscription is like this:

/**
 * @return mixed
 */
public function subscriptions()
{
    return $this->morphMany(Subscription::class, 'subscription');
}
0 likes
4 replies
SaeedPrez's avatar

Perhaps if you add more lines to the comments, and make each line 3 characters shorter than the previous.

1 like
rjvandoesburg's avatar

'More Laravel way' - In my eyes this is already very Laravel

What is it you are trying to accomplish? I suppose subscribe is a method in a controller? One could create a new method on forum like Forum::subscribe($user) and than that method will create a subscriber and add it to the subscribers of the forum ($this->subscribers()->save($subscription))

But that all depends on the application.

Please or to participate in this conversation.