0
votes

what is my mistake?

my controller :

public function index()
{
    $employees = Employee::all();
    return view('employee.view_all_employee_details', compact('employees'));
}

My Employee Model function :

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

My EmployeeAdditionalDetail model :

public function employeeDetails()
{
    return $this->belongsTo('App\Employee' , 'emp_id' , 'emp_id');
}

My View

@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

My Error:

ErrorException in C:\xampp\htdocs\socialhub\app\Http\Controllers\EmployeeController.php line 22: Undefined property: Illuminate\Database\Eloquent\Collection::$basicdetails

1

1 Answers

1
votes

I suspect the issue is with the third parameter of $this->hasOne(..), it should be 'local_key', normally this would be 'id'.

Try removing the third parameter, or put the correct local_key:

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