【问题标题】:Laravel 5.1 eloquent query syntaxLaravel 5.1 雄辩的查询语法
【发布时间】:2015-12-19 21:08:42
【问题描述】:

我有以下模型关系。如果用户以员工身份登录,我希望他们能够获得其公司的员工列表以及他们被分配的角色:

class User {

   // A user can be of an employee user type
   public function employee()
   {
       return $this->hasOne('App\Employee');
   }

   // 
   public function roles()
   {
      return $this->belongsToMany('App\Role');
   }

 }

 class Employee {

    // employee profile belong to a user
    public function user()
    {
       return $this->belongsTo('App\User');
    }

    // employee belongs to a company
    public function company()
    {
       return $this->belongsTo('App\Company');
    }
 }


 class Company {

    public function employees()
    {
        return $this->hasMany('App\Employee'); 
    }
 }

但是下面的查询不起作用。我收到错误Column not found: 1054 Unknown column companies.id in WHERE clause:

    $employee = Auth::user()->employee;

    $companyEmployees = Company::with(['employees.user.roles' => function ($query) use ($employee) {
        $query->where('companies.id', '=', $employee->company_id)
              ->orderBy('users.created_at', 'desc');
     }])->get();     

users 和 employees 表具有一对一的关系。
所有员工都有employee 的基本角色类型,此外他们还可能有其他角色,例如managersupervisor 等。

如何编写一个查询,为我提供一家包含所有员工及其角色的公司?

我尝试向 Company 模型添加 hasManyThrough 关系,但这也不起作用?

public function users()
{
    return $this->hasManyThrough('App\User', 'App\Employee');
}

【问题讨论】:

  • 您已经将角色作为声明函数,为什么要在 $users 查询中覆盖它?

标签: php mysql sql eloquent laravel-5.1


【解决方案1】:

我认为您正在为当前用户获取同事列表并急切地加载用户和角色?

$employee = Auth::user()->employee;
$companyEmployees = Company::with(['employees.user.roles')->find($employee->company_id); 

或许:

$companyEmployees = Company::find($employee->company_id)->employees()->with('user.roles')->get();
$sorted = $companyEmployees->sortBy(function($employee){ return $employee->user->created_at; }); 

这可能是更直接的路线。您的员工 ID 是否在用户表中,反之亦然?雄辩的关系很容易倒退。

【讨论】:

  • 第二种方法不错。但是你怎么能把它作为employee.created_at desc 来订购呢?
  • 您可以通过回调对集合进行排序。检查我的编辑。
【解决方案2】:
Users::select('table_users.id')->with('roles')->join('table_employes', function($join) use ($employee) {

    $join->on('table_employes.user_id','=','table_users.id')->where('table_employes.company_id', '=', $employee->company_id);

})->orderBy('tables_users.created_at')->get();

【讨论】:

  • 员工模型与角色没有关系。正是 User 模型与角色有关系,那么您如何使用 Employee 模型急切地加载角色?您需要按如下方式嵌套它with('user.roles')->get()
  • 你会如何申请orderby?
  • 对不起,我误解了订单的事情。 table_employees.user_id 和 tables_users.id 必须是相同的字段数据类型;
【解决方案3】:

1.在迁移中为数据库表列创建关系:

用户角色

$table->foreign('user_id')->references('id')->on('users');

用户

$table->increments('id');

2。为每个数据库表创建一个模型来定义关系

User.php(模型)

    public function userRoles()
    {
        return $this->hasOne('App\UserRoles', 'user_id', 'id');

    }

Userroles.php(模型)

public function user()
    {
        return $this->belongsTo('App\User', 'user_id', 'id');
    }

3。让控制器处理推荐使用 REST api 的数据库调用

控制器

    use App\User;
    use App\UserRoles;

 class UserController extends Controller
{

    public function index()
        {
            return User::with('userRoles')->orderBy('users.created_at', 'desc')->paginate(50);
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-26
    • 2021-07-23
    • 1970-01-01
    • 2017-11-11
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多