2
我的錯誤是什麼?試圖獲得非對象laravel的屬性5 - 我的錯誤是什麼?
Employeecontroller:
public function index()
{
$employees = Employee::all();
return view('employee.view_all_employee_details', compact('employees'));
}
員工型號功能:
protected $primaryKey = "emp_id";
protected $with = 'additionaldetails';
public function additionaldetails()
{
return $this->hasOne('App/EmployeeAdditionalDetail' , 'emp_id' , 'emp_id');
}
EmployeeAdditionalDetail型號:
public function employeeDetails()
{
return $this->belongsTo('App\Employee' , 'emp_id' , 'emp_id');
}
view_all_employee_details查看
@if(count($employees) > 0)
@foreach($employees as $employee)
<tr>
<td> {{ $employee->first_name }} </td>
<td> {{ $employee->manager_id }} </td>
<td>
{{ $employee->additionaldetails->emp_id }} **Error showing here**
</td>
</tr>
@endforeach
@else
{!! "<tr><td>No Recod Found</td></tr>" !!}
@endif
MyError:
Trying to get property of non-object
也僱員模型'保護$與= 'additionaldetails';'應'與=陣列( 'basicdetails')保護$; ' –
謝謝shuvrow。我已更改爲{{$ employee-> basicdetails-> emp_id}}。但仍然是同樣的問題。 – sankaran
@慢loris.thanks。但仍面臨同樣的問題。 :( – sankaran