简体   繁体   English

Laravel 5.3的关系

[英]Laravel 5.3 relationship

Here is my code: 这是我的代码:

class Company extends Model {
    public function employees() {
        return $this->hasMany('Employee');   
    }
}

class Employee extends Model{
    protected $table = "user_role_company";
    public function user(){
        return $this->belongsTo('User');
    }
    public function company(){
        return $this->belongsTo('Company');
     }
}

class User extends Authenticatable {
    public function employee(){
        return $this->hasMany('Employee');
    }
}

If I run: 如果我跑:

$recordsTotal = User::with(['employee' => function ($query) use ($company_id) {
    $query->where('company_id', $company_id);
}])->count();

It returns all the users count not only the empolyee count. 它返回所有用户不仅计算empolyee计数。

What am I doing wrong? 我究竟做错了什么?

Thanks 谢谢

First of all, do you have a 'company_id' and a 'user_id' column in the Employee model/DB? 首先,您在Employee model / DB中有'company_id'和'user_id'列吗?

Anyways, you can try this query instead. 无论如何,您可以尝试此查询。

$recordsTotal = User::whereHas('employee', function ($q) use ($company_id) {
     $q->where('company_id', $company_id);
})->count();

创建关系时,您需要引用整个命名空间或类User :: Class

When using relationships you can declare the Modal::class on the relationship given that the primary key and local key of both tables reference the correct values so for example you could have: 在使用关系时,您可以在关系上声明Modal :: class,因为两个表的主键和本地键都引用了正确的值,例如,您可以:

class Company extends Model {
    public function employees() {
        return $this->hasMany(Employee::class, 'company_id');   
    }
}

This will reference the company_id column on the Employee table or what ever table you have set within the Employee Modal. 这将引用Employee表上的company_id列或您在Employee Modal中设置的表。

You can also pass the foreign_key, local_key to the relation like so: 您也可以将foreign_key,local_key传递给关系,如下所示:

class Company extends Model {
   public function employees() {
    return $this->hasMany(Employee::class, 'foreign_key', 'local_key');   
   }
}

A similar example would be: 一个类似的例子是:

class Company extends Model {
   public function employees() {
    return $this->hasMany(Employee::class, 'company_id', 'company_id');   
   }
}

Depending on your database structure you should be able to just pass the modal::class to the relation however it is difficult to give a perfect working example without knowing your exact database structure and how you have your keys / identifiers setup, Meaning for example is your primary key named id or company_id. 根据您的数据库结构,您应该只能将modal :: class传递给关系,但是如果不知道您的确切数据库结构以及如何设置键/标识符,很难给出一个完美的工作示例。例如,意思是您的主键名为id或company_id。 Depending on that structure you can give this a shot. 根据这种结构,你可以尝试一下。

class Company extends Model {
public function employees() {
    return $this->hasMany(Employee::class);   
}

} }

Also remember when returning a collection to either loop over the array or simply use $company->employees()->get() another option would be to use ->first() instead of get() to use the first record in the array. 还记得在将一个集合返回到数组上的循环或者只是使用$ company-> employees() - > get()时,另一个选项是使用 - > first()而不是get()来使用第一个记录。阵列。

Hope this helps, I am no means an expert and still learning laravel my self, if you have any further issues with this relation or cant get the code to work i will try help where i can. 希望这有帮助,我不是一个专家,仍然学习laravel我自己,如果你有这个关系的任何进一步的问题或不能让代码工作我会尽我所能帮助。

Also worth taking a look here: Laravel Docs (Eloquent: Relationships) 另外值得一看: Laravel Docs(Eloquent:Relationships)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM