简体   繁体   English

有没有办法在 Laravel 7 中访问远程关系?

[英]Is there a way to access distant relations in Laravel 7?

I have four tables and somehow in my Payrolls table, I want to select an employee and fill out the details of his/her payroll.我有四个表,不知何故在我的工资表中,我想 select 一名员工并填写他/她的工资单的详细信息。 The only problem is that I can't get the employee position details because it is not connected directly to Payrolls table.唯一的问题是我无法获取员工 position 的详细信息,因为它没有直接连接到工资表。

在此处输入图像描述

Employees Model员工 Model

class Employees extends Model
{
protected $fillable = [
    'fname',
    'lname',
    'gender',
    'address',
    'positions_id'
];

public function positions()
{
    return $this->belongsTo(Positions::class);
}

public function payrolls()
{
    return $this->belongsToMany(Payrolls::class);
}
}

Positions Model位置 Model

class Positions extends Model
{
protected $fillable = [
    'name',
    'basic_pay'
];

public function employees()
{
    return $this->hasMany(Employees::class);
}
}

Payrolls Model工资单 Model

class Payrolls extends Model
{
protected $fillable = [
    'days_work',
    'overtime_hrs',
    'late',
    'absences',
    'bonuses',
    'employees_id'
];

public function employees()
{
    return $this->belongsToMany(Employees::class);
}
}

Is there a way where I can connect Payrolls and Positions?有没有办法可以连接工资单和职位? I want to get the basic_pay from the Positions table and display it on the Payrolls table (maybe through an Intermediate table).我想从 Positions 表中获取 basic_pay 并将其显示在 Payrolls 表中(可能通过中间表)。

I figured it out.我想到了。

I used query builder to join the three tables(employees, payrolls, positions) on the Controller:我使用查询生成器加入 Controller 上的三个表(员工、工资单、职位):

public function show(Employees $employee, Payrolls $payroll)
{
    $basic_pay = DB::table('payrolls')
    ->join('employees', 'employees.id', '=', 'payrolls.employees_id')
    ->join('positions', 'positions.id', '=', 'employees.positions_id')
    ->select('positions.basic_pay')
    ->where('payrolls.employees_id', '=', $payroll->employees_id)
    ->get();

    $position_name = DB::table('payrolls')
    ->join('employees', 'employees.id', '=', 'payrolls.employees_id')
    ->join('positions', 'positions.id', '=', 'employees.positions_id')
    ->select('positions.name')
    ->where('payrolls.employees_id', '=', $payroll->employees_id)
    ->get();

    return view('payrolls.show')
    ->with('payrolls', $payroll)
    ->with('basic_pay', $basic_pay)
    ->with('position_name', $position_name)
    ->with('employees', $employee);
}

And on my views:根据我的观点:

<label><h4>Position: </h4></label>
<span style="font-size: 25px">
   {{ $position_name->pluck('name')->first() }}
</span><br>

<label><h4>Basic Pay: </h4></label>
<span style="font-size: 25px">
   {{ $basic_pay->pluck('basic_pay')->first() }}
</span><br>

I used pluck to pluck the desired column field that I want to show.我使用 pluck 来提取我想要显示的所需列字段。

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

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