简体   繁体   中英

Laravel Repositories and multiple related models

I'm currently just starting on porting a large Yii2 application to Laravel and I'm considering using repositories to solve my current problem of messy controllers and bloated models.

I've looked at a ton of examples of repositories but most them only cover simple CRUD. For example in my current app a User can created in multiple places and must be created with like 5 related records in other tables, for example a User must be added to Groups and must have Permission records created based on their role.

Currently almost all of my models have a custom Create function that can reused anywhere. Here is a very basic Yii2 example of what I'm talking about.

Example User creation in User model:

 public static function create(array $attributes, array $group_ids)
{
    $user = new User;
    $user->attributes = $attributes;
    $user->save();

    $role = $user->role;

    foreach ($role->rolePermissions as $role_permission) {
        UserPermission::create($user->id, $role_permission);
    }

    foreach ($group_ids as $group_id) {
        GroupUser::create($user->id, $group_id)
    }

    if ($role->admin_mode) {
         // send welcome email
    }
    return $user;
}    

Is it good practice to simply inject the UserPermission and GroupUser Repositories into my UserRepository in this case? How do you handle saving related records in Repositories?

Does anyone have any more advanced examples of using Repositories with related models?

You need to understand that in Repository pattern, repository is not the place to store your business logic, that's why they are simple, doing CRUD only.

For what you need here is a business logic layer on top of the repositories. What I did to my recent projects is that I create a Services folder in my app root, and create all sort of services in it with my main business logic in it, like $userService->createUser(..) , and in it will have checks on different roles, groups, emitting events, send emails.

So a typical calls could look like this:

  • calls UsersController->createUser

    • calls UserService ->createUser

      • verifications, checks, throws errors
      • calls UserRepository->create
      • calls GroupUserRepository->create
      • emit UserCreated event
    • (disjointed: event listeners)

      • dedicated listeners listened UserCreated event
        • calls UserService ->sendWelcomeEmail
          • send welcome emails

So services is almost identical to your fat models, but they are standalone php classes that understand your business logic, use whatever database it knows, and process requests and return results. It does not depend on anything to instantiate one, and so can be included by anyone to use it. They also can be instantiated as singleton or instances depending on your need.

Side note

I've wasted too much time on creating repositories when I could have just use Eloquent. They are not identical, but I could not foresee my project to change underlying DBMS so I would usually just use eloquent directly in my services layer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM