1
在這裏我試圖循環所有我的用戶與他們的相關任務。我在模型中創建了用戶 - >分配關係。當我dd($學生)時,我收到學生的數組及其相關的作業,但是當我在頁面上查看時,我沒有收到作業循環。我在代碼中錯過了什麼?我確定它是在我搞砸了的HTML中的某個地方。循環對象關係Laravel
用戶模型:
public function assignments()
{
return $this->hasMany('App\Assignment');
}
分配模型:
public function userAssignment()
{
return $this->belongsTo('App\User');
}
控制器:
$students = User::where('position','student')->with('assignments')->get();
HTML:
@foreach($students as $student)
<div class="item{{{ $isFirst ? ' active' : '' }}}">
<div class = "deck">
<div class = "row marg">
<div class="col-md-8">
<div class = "stdnt">
<h1>Student Info</h1>
<h1>{{$student->name}}</h1>
<p>Program: {{$student->pack}}</p>
<p>Level: {{$student->level}}</p>
<p>Status: {{$student->status}}</p>
<p>Lesson: {{$student->lesson}}</p>
</div>
</div>
<div class = "col-md-4">
<div class = "teac">
<h1>Teacher Info</h1>
<h1>{{$student->teacher}}</h1>
<p>Picture</p>
<p>assign form</p>
<p>assign button</p>
</div>
</div>
</div>
</div>
<div class = "mask flow">
<div class = "row-fluid marg">
@foreach($student->assignments() as $assignment)
<div class = "ablock">
<h1>{{$assignment->title}}</h1>
<p>{{$assignment->created_at}}</p>
<p>{{$assignment->type}}</p>
<p>{{$assignment->body}}</p>
</div>
@endforeach
</div>
</div>
{{--*/ $isFirst = false; /*--}}
</div>
@endforeach
是的。這工作。這是小事情。多謝約瑟夫。 –