我想从部门表中获得department_name
table department = id, department_name, total_employee
table employee = id, employee_name, id_department, email, telephone, gender, status我试过model.Employee
public function department()
{
return $this->belongsTo(Department::class);
}controllers.EmployeeControllers
public function index()
{
$data_employee = Employee::with('department')->get();
return view ('employee.index', compact('data_employee '));
}有视野
@forelse ($data_employee as $item)
<tr>
<td class="text-center">{{ $item->employee_name}}</td>
<td class="text-center">{{ $item->department->department_name}}</td>
<td class="text-center">{{ $item->email}}</td>
<td class="text-center">{{ $item->telephone}}</td>
</tr>
@endforelse但上面写着
尝试在null上读取属性"department_name“
我做错什么了。
发布于 2022-09-28 11:55:00
请更换model.Employee
public function department()
{
return $this->belongsTo(Department::class, 'id_department');
}https://stackoverflow.com/questions/73880755
复制相似问题