[英]Laravel 5.4 Relationships between 3 tables
I am new in laravel, I already know how to join tables using the query builder. 我是laravel的新手,我已经知道如何使用查询生成器联接表。 I just like to learn how to use relationships to avoid repetition of codes and to simplify it.
我只是想学习如何使用关系来避免重复代码并简化代码。 I can join 2 tables, but I can't get the 3rd table.
我可以加入2张桌子,但不能获得3张桌子。
I like to display employees assigned tasks information from the Tasks
table, this table only has the project id that needs to be joined to the Projects
table. 我想显示“
Tasks
表中分配给员工的任务信息,该表仅具有需要加入到“ Projects
表中的项目ID。 Other employees can join in existing projects with other employees. 其他员工可以与其他员工一起加入现有项目。
Employee
model: Employee
模式:
public function tasks()
{
return $this->hasMany('\App\Task', 'project_coder', 'id');
}
Task
model: Task
模型:
public function projects()
{
return $this->hasMany('\App\Project', 'id', 'project_id');
}
Projects
model: Projects
模型:
public function belongsToTasks()
{
return $this->belongsToMany('\App\Task', 'project_id', 'id');
}
I can only get the IDS from Task model. 我只能从任务模型中获取IDS。 the ID will be use to fetch the project info from project tables.
该ID将用于从项目表中获取项目信息。 Unfortunately I cant do that part.
不幸的是,我不能做到这一点。
Employees controller: 员工控制人:
public function show($id)
{
$data = Employees::find($id);
return view('show-employee')->withInfo($data);
}
Is it good practice to use query builder rather than relationships? 使用查询生成器而不是关系是一种好习惯吗?
UPDATE : 更新 :
Employees
table Employees
表
{
"id":1,
"name": "Juan"
}
Tasks
table Tasks
表
{
"id":1, // autoincrement and will not be use for linking to other tables.
"project_id": 1, //use to connect to project table
"project_coder": 1 // use to connect to employees table
}
Projects
table Projects
表
{
"id":1,
"name": "First Project"
}
To deal with this is best to create pivot table like employee_task
where table will have just two columns task_id
and employee_id
为了解决这个问题,最好创建像
employee_task
这样的数据透视表,其中表只有两列task_id
和employee_id
then you can define in Task model 然后您可以在任务模型中定义
public function employees()
{
return $this->belongsToMany('\App\Employee', 'employee_task');
}
and Employee model 和员工模型
public function tasks()
{
return $this->belongsToMany('\App\Task', 'employee_task');
}
now you can see all employee tasks and rest of you relations work just fine 现在您可以看到所有员工的任务,其余的人际关系也很好
$data = Employees::find($id);
$data->tasks //you get all tasks assigned to this employee
$data->tasks()->first()->projects //you get all your projects for first task
$data->tasks()->first()->employees //all employees assigned to first task with this employee
Also recommend to you to lookup pivot tables, attach()
, detach()
and sync()
functions. 还建议您查找数据透视表,
attach()
, detach()
和sync()
函数。 you can check it here : https://laravel.com/docs/5.7/eloquent-relationships#updating-many-to-many-relationships 您可以在这里查看: https : //laravel.com/docs/5.7/eloquent-relationships#updating-many-to-many-relationships
UPDATE: 更新:
Ok I understand now what you are trying to do. 好的,我现在知道您正在尝试做什么。 You already have pivot table.
您已经有数据透视表。 I was little confused with your Project model which is not necessary.
我对不必要的您的Project模型有点困惑。
You can remove your Project
class if you have it just for this relation and update your model relations as I wrote above. 您可以删除
Project
类(如果仅具有该关系),并更新模型关系,如我上面所写。 You don't need project_id
and project_coder_id
in this relation. 在这种关系中,您不需要
project_id
和project_coder_id
。 Also change the column names to more conventional names like employee_id
and task_id
as I mentioned or whatever your table names are. 还要将列名更改为更常规的名称,如我提到的
employee_id
和task_id
或任何表名。 And you can rename employee_task
pivot table name to your project table or you can rename it as well. 您可以将
employee_task
数据透视表名称重命名为项目表,也可以重命名。
EDIT 编辑
When you use Project model for another data, you need to create 4th table as I mentioned above. 当您将Project模型用于其他数据时,您需要如上所述创建第4个表。
FINAL 最后
project_coder
column in Tasks
table - unecessary column Tasks
表中删除project_coder
列-不必要的列 employees_task
with employee_id
, task_id
employee_id
和task_id
创建数据透视表employees_task
I assume that Project
hasMany()
tasks and Task
only belongsTo()
one project. 我假设
Project
hasMany()
任务和Task
仅belongsTo()
一个项目。 So need to create these relations as well. 因此也需要创建这些关系。
Then you can use these relations like this: 然后,您可以使用以下关系:
$employee = Employee::find($id);
$task = Task::find($id);
$project = Project::find($id);
$employee->tasks //all tasks assigned to employee
$task->employees //all employees assigned to task
$task->project //project info assigned to task
$employee->tasks()->first()->project //project data from first task of employee
$project->tasks()->first()->employees //all employees assigned to first project task
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.