【问题标题】:Unable to display relations data from eloquent relationship无法显示来自 eloquent 关系的关系数据
【发布时间】:2017-06-05 17:51:39
【问题描述】:

我试图在我的视图中显示来自雄辩关系的数据,但我似乎做错了一点。 dd 显示集合中的关系,但我只是无法在我的视图中正确调用数据。以下是我所做的

员工模型

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

任务模型

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

任务控制器

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

    dd($alltask);

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

任务视图

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

我无法显示 $task->firstname,名字来自雇员表。下面是 dd 的结果截图

如何显示员工的名字?

【问题讨论】:

    标签: laravel-5


    【解决方案1】:
    $task->employee->firstname
    

    【讨论】:

    • 修复了它。现在因为周末没有尝试这种方式而殴打自己。谢谢
    猜你喜欢
    • 2021-12-13
    • 2021-12-05
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多