【问题标题】:Join 3 tables based on column values - Laravel根据列值连接 3 个表 - Laravel
【发布时间】:2020-01-31 09:33:54
【问题描述】:

线索表

  • 身份证
  • 标题
  • owner_id
  • from_table

员工表

  • 身份证
  • 名字
  • 姓氏
  • 角色

管理表

  • 身份证
  • 名字
  • 姓氏
  • 角色
$users = Leads::query();

return Datatables::make($users)

    ->editColumn('owner_id', function ($user) {
        if($user->from_table == 'employee'){
            $emp = Employee::where('id',$user->owner_id)->first();
            return $emp->first_name.' '.$emp->last_name.' ('.$emp->role.')';
        }
        if($user->from_table == 'admin'){
            $admin = Admin::where('id',$user->owner_id)->first();
            return $admin->first_name.' '.$admin->last_name.' ('.$admin->role.')';
        }
    })

上述解决方案运行良好,但我们无法在数据表中按列搜索。

我想要的是加入查询,例如:

如果(leads.from_table == 员工) // 从 EMPLOYEE TABLE 获取数据,即 LEADS TABLE + EMPLOYEE TABLE

  • 身份证
  • 标题
  • owner_id
  • from_table
  • 名字
  • 姓氏
  • 角色

如果(leads.from_table == 管理员) // 从 ADMIN TABLE 获取数据,即 LEADS TABLE + ADMIN TABLE

  • 身份证
  • 标题
  • owner_id
  • from_table
  • 名字
  • 姓氏
  • 角色

【问题讨论】:

  • 除非管理员不是员工,否则我认为我不会将它们存储在单独的表中(至少不是他们的名字等)
  • 是的,管理员不是员工,但他们可以将任何潜在客户分配给员工。

标签: mysql laravel eloquent


【解决方案1】:

我认为你应该改变你的数据库结构以使用多态关系,它们完全符合你的需求 - https://laravel.com/docs/5.8/eloquent-relationships#polymorphic-relationships

【讨论】:

    【解决方案2】:

    from_table 列应该包含父模型的类名。

    Add in Leads model
    
        public function fetchOwner()
        {
            return $this->morphTo();
        }
    
    

    添加员工模型

       public function employee()
        {
            return $this->morphOne('App\Employee', 'fetchOwner');
        }
    
    

    添加管理模型

    public function employee()
        {
            return $this->morphOne('App\Admin', 'fetchOwner');
        }
    
    
    $users = Leads::with('fetchOwner');
    return Datatables::make($users)
    
        ->editColumn('owner_id', function ($user) {
           return $user->fetchOwner->name;
        })
    
    

    【讨论】:

    • 错误:异常消息:试图获取非对象的属性“first_name”
    • dd() $users 并验证 'fetchOwner' 是否到来。from_table 列应该包含父模型的类名。
    • 在 fetchOwner 中显示 null。顺便说一句,我已经通过修改我的代码和数据库来解决它。
    【解决方案3】:

    感谢所有试图提供帮助的人..

    我正在回答我自己的问题,因为我在到处挖掘 9 天后找到了答案..

    这就是答案:

    you may want to replace owner_code by owner_id in your business table.

    所以我将 from_table 更改为 owner_type & owner_type 现在应该包含类名作为值 ex:将 admin 更改为 App\Admin & employee 到 App\Employee in Database

    App\Admin.php
    
    public function employee()
        {
            return $this->morphOne('App\Leads', 'owner');
        }
    
    
    App\Employee.php
    
    public function employee()
        {
            return $this->morphOne('App\Leads', 'owner');
        }
    
    App\Leads.php
    
    public function owner()
        {
            return $this->morphTo();
        }
    

    感谢:laravel morphTo how to use custom columns? (EddyTheDove) 指出了确切的问题..

    【讨论】:

      猜你喜欢
      • 2021-01-14
      • 1970-01-01
      • 2018-08-27
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多