There is a library project on Laravel 5.2 . There is a standard App\User model. It now contains all the methods for relationships and this model looks like this:

 class User extends Authenticatable
 {
     / **
      * @return \ Illuminate \ Database \ Eloquent \ Relations \ BelongsToMany
      * /
     public function roles ()
     {
         return $ this-> belongsToMany (Role :: class);
     }

     / **
      * Comments written by user
      *
      * @return \ Illuminate \ Database \ Eloquent \ Relations \ HasMany
      * /
     public function comments ()
     {
         return $ this-> hasMany (Comment :: class);
     }

     / **
      * Books read by user
      *
      * @return \ Illuminate \ Database \ Eloquent \ Relations \ BelongsToMany
      * /
     public function books ()
     {
         return $ this-> belongsToMany (Book :: class) -> loadRating () -> withPivot ('finished');
     }

     / **
      * All books, that user is waiting for
      *
      * @return \ Illuminate \ Database \ Eloquent \ Relations \ BelongsToMany
      * /
     public function expectations ()
     {
         return $ this-> belongsToMany (Book :: class, 'user_book_line');
     }

     / **
      * Confirmations
      *
      * @return \ Illuminate \ Database \ Eloquent \ Relations \ BelongsToMany
      * /
     public function confirmations ()
     {
         return $ this-> belongsToMany (Book :: class, 'books_confirmations');
     }
 }

All the methods that are associated with the interaction of the user and books, at the moment, I made a separate treit, but I think this is wrong. I want all user-related logic in a separate model, for example, in the app/Library/User.php .
Further expansion of the project (adding new features, etc.) will lead to cluttering the User model with all the functionality, and this I need to avoid.

In the normal inheritance of the base User class, there is a problem - the call to Auth::user() will return exactly the base User class.
Prompt how it will be more correct to organize structure of inheritance in this case?

    2 answers 2

    Make inheritable classes and method in User

     public function getSomeClass() { return SomeClass::find($this->id); } 

    either so you can use eagerLoading

     public function getSomeClass() { return $this->hasOne('SomeClass'); } 

    Or you can extend the class Auth, register it in aliases, and add there methods to call the right class Auth::someRole() right away, for more, Auth::someRole() here .

      You can specify any class in the config/auth.php in providers.users.model and it will be used by the system for authentication.

      In laravel much can be configured through configs and what remains will be configured through service providers :)

      • I need to specify several classes in the config file. Suppose I want not only the class App\Library\User , but also App\Mail\User , which will contain methods for the user to work with mail. So I can implement it? - malyusha