Php Laravel 5未定义的属性:Illumb\Database\Eloquent\Collection::$basicdetails
我犯了什么错 我的控制器:Php Laravel 5未定义的属性:Illumb\Database\Eloquent\Collection::$basicdetails,php,laravel-5,eloquent,relationship,Php,Laravel 5,Eloquent,Relationship,我犯了什么错 我的控制器: public function index() { $employees = Employee::all(); return view('employee.view_all_employee_details', compact('employees')); } 我的员工模型功能: public function basicdetails() { return $this->hasOne('App/EmployeeAdditionalDe
public function index()
{
$employees = Employee::all();
return view('employee.view_all_employee_details', compact('employees'));
}
我的员工模型功能:
public function basicdetails()
{
return $this->hasOne('App/EmployeeAdditionalDetail' , 'emp_id' , 'emp_id');
}
My EmployeeAdditionalDetail模型:
public function employeeDetails()
{
return $this->belongsTo('App\Employee' , 'emp_id' , 'emp_id');
}
我的看法
@if(count($employees) > 0)
@foreach($employees as $employee)
<tr>
<td> {{ $employee->first_name }} </td>
<td> {{ $employee->manager_id }} </td>
<td>
{{ $employee->basicdetails->personal_email }} </p> **Error showing here**
</td>
</tr>
@endforeach
@else
{!! "<tr><td>No Recod Found</td></tr>" !!}
@endif
@if(计数($employees)>0)
@foreach($employees作为$employees)
{{$employee->first_name}
{{$employee->manager\u id}
{{$employee->basicdetails->personal_email}**此处显示错误**
@endforeach
@否则
{!!“未找到记录”!!}
@恩迪夫
我的错误:
错误异常
C:\xampp\htdocs\socialhub\app\Http\Controllers\EmployeeController.php
第22行:未定义的属性:
Illumb\Database\Eloquent\Collection::$basicdetails
我怀疑问题在于$this->hasOne(..)的第三个参数,它应该是'local_key',通常是'id' 尝试删除第三个参数,或输入正确的本地\u键:
public function basicdetails()
{
return $this->hasOne('App/EmployeeAdditionalDetail' , 'emp_id');
}
是的,我已经搬走了。现在出现错误:“尝试获取非对象的属性”