Laravel 5 Undefined属性:Illuminate \ Database \ Eloquent \ Collection :: $ basicdetails

时间:2016-01-20 06:15:46

标签: 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/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 \程序\ HTTP \控制器\ EmployeeController.php   第22行:未定义的属性:   照亮\数据库\雄辩\收藏:: $ basicdetails

1 个答案:

答案 0 :(得分:0)

我怀疑问题是$ this-&gt; hasOne(..)的第三个参数,它应该是'local_key',通常这将是'id'。

尝试删除第三个参数,或者输入正确的local_key:

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