doudi5892
2015-12-19 21:08
浏览 91
已采纳

Laravel 5.1雄辩的查询语法

I have the following model relationships. If a user logs in as an employee, I want them to be able to get a list of employees for a their company and the roles they have been assigned:

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'); 
    }
 }

But the following query doesnt work. I get error 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();     

The users and the employees table have a one to one relationship.
All employees have a base role type of employee in addition they may also have other roles such as manager, supervisor etc.

How do I write a query that gives me a company with all its employees and their roles?

I've tried to add a hasManyThrough relation to the Company model but that doesn't work either?

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

图片转代码服务由CSDN问答提供 功能建议

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

  class User {  
 
 //用户可以是员工用户类型
公共职能员工()
 {
返回$ this-> hasOne('App \ Employee'); 
} 
 
 / n /  / 
公共职能角色()
 {
返回$ this-> belongsToMany('App \ Role'); 
} 
 
} 
 
类员工{
 
 // employee 个人资料属于用户
公共职能用户()
 {
返回$ this-> belongsTo('App \ User'); 
} 
 
 //员工属于公司
公共职能 company()
 {
返回$ this-> belongsTo('App \ Company'); 
} 
} 
 
 
类公司{
 
公共职能employees()
 {  
返回$ this-> hasMany('App \ Employee');  
} 
} 
   
 
 

但以下查询无效。 我收到错误找不到列:1054 WHERE子句中的未知列companies.id

  $ 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();  
   
 
 

用户和员工表具有一对一的关系。
所有员工的基本角色类型为 employee 此外,他们还可能有其他角色,如 manager supervisor 等。

我如何编写一个查询给我一个 公司及其所有员工及其角色?

我尝试将hasManyThrough关系添加到公司模型,但这也不起作用? < pre> public function users() { 返回$ this-&gt; hasManyThrough('App \ User','App \ Employee'); } \ n

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dtnnpt11795 2015-12-20 15:34
    已采纳

    I think you're ring to get a list of coworkers for the current user and eager load the user and role?

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

    Or perhaps:

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

    That might be a more direct route. Is your employee id in the user table or vice versa? The eloquent relationships are easy to set backwards.

    已采纳该答案
    打赏 评论
  • dou70260 2015-12-19 21:26
    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();
    
    打赏 评论
  • dqluw20882 2015-12-19 21:28

    1. Create relationship for database table columns in migrtaion :

    User Role

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

    Users

    $table->increments('id');
    

    2. Create a model for each database table to define relationship

    User.php (model)

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

    Userroles.php (model)

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

    3. Let controller handle database calls recommended to use REST api

    Controller

        use App\User;
        use App\UserRoles;
    
     class UserController extends Controller
    {
    
        public function index()
            {
                return User::with('userRoles')->orderBy('users.created_at', 'desc')->paginate(50);
            }
    }
    
    打赏 评论

相关推荐 更多相似问题