Currently I tend to follow a Controller, Service, Model pattern in my projects.
Controller
Nothing special here, just the standard Laravel controller class existing at a one to one ratio with my Models.
Service Layer
A service layer existing again at a one to one ratio with my Models, all of which extend a generic service class that does simple CRUD operations.
Model
Standard Laravel Eloquent model class including relationships.
Generally speaking this is organized and predictable enough but has lend to some confusion in certain relationship situations. For example, Let's say you have a "Team" object which is made up of "User" objects.
Does it make more sense to call the "User" service with...
$userService->users($teamId);
$userService->getById($userId);
$userService->create($teamId, $attributes);
or call the "Team" service class with...
$teamService($teamId)->users();
$teamService($teamId)->getUserById($userId);
$teamService($teamId)->createUser($attributes);
Putting all that logic under the “Team” service seems easier to read/understand but the downside is that you end up with a really fat service class, especially if the “Team” object owns lots of different objects (statuses, tags, comments, posts, etc).
Should I have a combination? For example…
$teamService($teamId)->users();
Actually calls something like this…
public function users()
{
$userService = new UserService();
$users = $userService->find(‘team_id’, ‘=‘, $this->$team->id);
return $users;
}
The same could be asked for the Controllers really. Would love to get some feedback from others that use a similar pattern.