lundi 5 juin 2017

Unable to display relations data from eloquent relationship

I'm trying to display in my view data from an eloquent relationship but i seem to be doing a tiny bit wrong. dd shows the relation in the collection but i just can't call the data correctly in my view. Below is what i have done

Employee model

public function task()
{
    return $this->hasMany(Task::class);
}

Task model

public function employee()
{
    return $this->belongsTo(Employee::class);
}

TaskController

public function index()
{
    $alltask = Task::with('employee')->get();

    dd($alltask);

    /*return view('task.task', compact('alltask', 'empwithtask'));*/
}

task view

@foreach ($alltask as $task)
<tr>
    <td></td>
    <td></td> 
    /* this is meant to be the  employee.firstname */
    <td></td>
    <td></td>
</tr>
@endforeach

I'm not able to display $task->firstname, firstname is from the employees table. Below is a snapshot of the result of dd

dd capture

How do I show the employee firstname?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire