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?