当我想显示我的员工资料并想从另一个表添加部门和称号时,我遇到了这个问题。我解决不了。.我看到了之前的问题,但无法解决我的答案。
控制器:
public function show($id)
{
$data['title'] = "Profile";
$data['employee'] = Employee::with(['department','designation'])->findOrFail($id);
$data['departments'] = Department::orderBy('name')->pluck('name','id');
$data['designations'] = Designation::orderBy('name')->pluck('name','id');
return view('admin.employee.show',$data);
}
视图:
<li>
<div class="title">Department:</div>
<div class="text">{{ $employee->department->name }}</div>
</li>
型:
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentSoftDeletes;
use IlluminateNotificationsNotifiable;
class Employee extends Model
{
use SoftDeletes;
use Notifiable;
protected $fillable = [
'firstname','lastname', 'email', 'password','designation_id','department_id'];
public function designation()
{
return $this->belongsTo(Designation::class);
}
public function department()
{
return $this->belongsTo(Department::class);
}
}
部门型号:
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentSoftDeletes;
class Department extends Model
{
use SoftDeletes;
protected $fillable=['name','depatment_details'];
}
尝试获取非对象的属性"名称"(视图:C:\xampp\htdocs\hrm\resources\views\admin\employee\show.blade.php(
试试这个。
<li>
<div class="title">Department:</div>
<div class="text">{{ $data['employee']->department->name }}</div>
</li>