dongling0519 2017-04-20 11:27
浏览 82
已采纳

不同用户类型的Laravel关系

I have a platform where I have Users that are a Shop, they can place Orders, and I have Users that are Suppliers, they can view the placed Orders.

Now I'm running into some logic issue's, for example in my User class I woud like for the Supplier to be able to do this

Auth::user()->orders

But the User that is a shop should also be able to do this

Auth::user()->orders

I illustrated a bad example on how to do this here:

class User extends Authenticatable
{
  public function orders()
  {
      return $this->hasMany(Order::class, 'created_by');
  }

  public function alsoHasOrders()
  {
      return $this->hasMany(Order::class, 'fulfilled_by');
  }

}

There should be a better way to do this right?

  • 写回答

2条回答 默认 最新

  • douwen1213 2017-04-21 09:02
    关注

    For starters, I'm going to suggest you redesign your data model. Your User model is your Authenticator, and this is all it should be concerned with. It should not also be a shop or a supplier, or anything else. Supplier and Shop should be distinct models on their own which 'have' a User.

    What you're doing at the moment violates S.O.L.I.D. as do most of the answers to your question here. Specifically O or Open for extension, closed for modification. For example, what if a user is a shop AND a supplier? What if later down the line you add a Distributer type as well? You have to go back to that model and add more if/else statements, more types of relations etc etc...

    Instead, I would consider having roles of 'shop' and 'supplier' as well as distinct Shop and Supplier Models that have a User, which one or both of can be assigned to a user. You will likely already have the User object once authentication has been done, so after this point you can use a service retrieve Shop and Supplier information based on that users assigned roles.

    Some quick sudo code of a basic implementation:

    class ShopService
    {
      public function listOrders(User $user)
      {
        if ($user->hasRole('shop')) {
          return Shop::with('orders')->where('user_id', $user->id)->get()
        }
    
        return null;
      }
    }
    
    class SupplierService
    {
      public function listOrders(User $user)
      {
        if ($user->hasRole('supplier')) {
          return Supplier::with('orders')->where('user_id', $user->id)->get()
        }
    
        return null;
      }
    }
    

    This means you can very easily extend this by adding a 'distributer' Role and a DistributerService

    However, If you are still determined to go down this route, at the very least I would suggest that you still have distinct Shop and Supplier models, that each extend the base User model, and then apply the relevant relations within each.

    For example:

    class User extends Authenticatable
    {
       ... user stuff
    }
    
    class Shop extends User
    {
      public function orders()
      {
          return $this->hasMany(Order::class, 'created_by');
      }
    }
    
    class Supplier extends User
    {
      public function orders()
      {
          return $this->hasMany(Order::class, 'fulfilled_by');
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。