panthro
panthro

Reputation: 24059

Defining a Relationship on User

I'm developing a package that is using the user model in the main app.

I need to add a relationship to the user model that links to one of my packages models.

How can I do this?

Create a new model in my package called user and namespace it and define the relationship here?

Is there a better way?

I do not wish for who ever uses the package to go in and add the relationship themselves to the user model.

Upvotes: 0

Views: 64

Answers (1)

Joseph Silber
Joseph Silber

Reputation: 220036

You should create a trait for that, and ask you package's users to add it in:

namespace MyAwesome\Package;

trait PerishableTrait {
    public function perishables()
    {
        return $this->hasMany('MyAwesome\Package\Perishable');
    }
}

Then tell your users to just add this single line to their User model:

class User extends ... {
    use MyAwesome\Package\PerishableTrait;
}

Upvotes: 2

Related Questions