2017-05-30 124 views
0

正如標題中提到的,我得到錯誤"Property [id] does not exist on this collection instance."只有當我在線運行代碼時,這裏是我的相關代碼。Laravel錯誤:此集合實例上不存在屬性[id]。但它在本地服務器上工作

1 EmployeeController(瀏覽器告訴我的錯誤是這裏的第二行)

public function show(Employee $employee) 
{ 
    $employee = Employee::find ($employee); 
    $edocument = EDocument::where ('employee_id',$employee->id)->first(); 
    return view ('employee.show')->withEmployee($employee)->withEdocument($edocument); 
} 

2 show.blade.php

<div class="jumbotron"> 
<h1>{{$employee->name}} ({{$employee->position}})</h1> 
@if (isset($edocument)) 
    <a href="{{route('employee-docs.show',$edocument->id)}}" class="btn btn-lg btn-primary">Go To Employee Database Page</a> 
@else 
    <p class="lead bg-danger">Employee documents are not uploaded</p> 
@endif 
<a href="{{route('getContract',$employee->id)}}" class="btn btn-success btn-lg">Create Employee Contract </a> 

如果任何人都可以解釋我更詳細的這個錯誤,這將是偉大的。感謝

PS ..這是我的第一個項目laravel(;

回答

0

您在控制器方法中使用路徑模型綁定來獲得Employee模型,但是您也運行find,這會失敗,因爲您傳遞模型而不是o f這個id。做爲下面顯示的代碼之一,不要混合它們。

如果要使用路由模型綁定,請執行此操作。

public function show(Employee $employee) 
{ 
    $edocument = EDocument::where ('employee_id', $employee->id)->first(); 

    return view ('employee.show')->with(compact('employee', 'edocument')); 
} 

如果要傳遞員工ID並在控制器中獲取模型,請執行此操作。

public function show($employee) 
{ 
    $employee = Employee::find($employee); 
    $edocument = EDocument::where ('employee_id', $employee->id)->first(); 

    return view ('employee.show')->with(compact('employee', 'edocument')); 
} 
+0

謝謝。是的,我做到了這一點,它的工作......我想我沒有注意到這個錯誤,因爲代碼在某種程度上在本地工作,這很奇怪。 –

0

也許這可以幫助你你爲什麼不通過在控制器中使用的信息 - 它爲我

return view('employee.show', ['employee' => $employee, 'edocument'=>$edocument]); 

。 。(不必改變show.blade.php中的任何內容)

相關問題