Home » Php » laravel In which model should I put an invite method (User oder Invitation)

laravel In which model should I put an invite method (User oder Invitation)

Posted by: admin October 26, 2017 Leave a comment

Questions:

Given I have two models User and Invitation

invitation only consists of user_id, invited_email

and I have a model method:

public function invite(string $email) {
    Invitation::create([
        'user_id' => auth()->user()->id,
        'invited_email' => $email
    ]);
}

Should this be in the Invitation or User model?
It would seem reasonable to be in the User model since it is an action that a user performs. However, my User model becomes very bloated when I follow this strategy for everything, since most actions are related to a user.

Answers:

I think it’s fine to be on the User model since you’re inviting the current loged in user (auth()->user()).

Maybe you can clean up some of that other logic you have on the User to another Model or Service that can be worth trying.