簡體   English   中英

Laravel 5未定義的屬性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ basicdetails

[英]Laravel 5 Undefined property: Illuminate\Database\Eloquent\Collection::$basicdetails

我怎么了

我的控制器:

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');
}

我的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

我的錯誤:

C:\\ xampp \\ htdocs \\ socialhub \\ app \\ Http \\ Controllers \\ EmployeeController.php第22行中的ErrorException:未定義的屬性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ basicdetails

我懷疑問題在於$ this-> hasOne(..)的第三個參數,它應該是'local_key',通常是'id'。

嘗試刪除第三個參數,或輸入正確的local_key:

public function basicdetails()
{
    return $this->hasOne('App/EmployeeAdditionalDetail' , 'emp_id');
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM