dpwdsmbvm496180204 2016-11-10 10:01
浏览 34
已采纳

如何区分配置文件?

I have two types of user: client, manager. are stored in separated tables.

Each of then have unique nickname.

So, I need to open profile by nickname.

For Client is:

$client = Client::where("nickname", $nickname)

For Manager is:

$manager = Manager::where("nickname", $nickname)

So, I try to make universal function that dewtect where is client and manager and execute the appropriate query.

How can I improve this code and detect type of user only by nickename?

  • 写回答

2条回答 默认 最新

  • douchuang8359 2016-11-10 10:06
    关注

    You should use one model for this, I guess it would be best way to handle clients and managers.

    If for some reason you want to use two models, you can create method and put it in Client model since most of queries will be for clients:

    public function getClientOrManagerByNickname($nickname) {
        $client = $this->where('nickname', $nickname)->first();
    
        return is_null($client) ? (new Manager)->where('nickname', $nickname)->first() : $client;
    }
    

    This code will create one query if client is found and will return this client. Or it will create two queires and will return manager. If there is no client and manager with this nickname, it will return null.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!