我正在嘗試訪問我的刀片模板中的模型。所以我可以循環徹底的數據。如何在刀片模板中引用我的模型
我已經打過電話全局函數一樣
{{ Auth::user }}
,它工作正常。我可以在我的視圖中輸出用戶數據。
我創建了一個名爲students的模型,該模型用來自用戶表的user_id保存學生數據。像1-> N關係一樣。用戶有多個學生與之相關聯。我如何在我的視圖中調用自定義模型。
我正在嘗試訪問我的刀片模板中的模型。所以我可以循環徹底的數據。如何在刀片模板中引用我的模型
我已經打過電話全局函數一樣
{{ Auth::user }}
,它工作正常。我可以在我的視圖中輸出用戶數據。
我創建了一個名爲students的模型,該模型用來自用戶表的user_id保存學生數據。像1-> N關係一樣。用戶有多個學生與之相關聯。我如何在我的視圖中調用自定義模型。
您可以創建一個變量,並傳遞給視圖:
$user = User::where('id', 1)->with('students')->first();
return view('some.view', compact('user'));
然後你就可以在視圖itearte過學生:
@foreach ($user->students as $student)
{{ $student->name }}
@endforeach
通行證學生數據,查看
$students = Student::all();
return view('student_view')
->with('student_data', $students);
查看同類表
<table id="table-student" class="table table-bordered table-striped">
<thead>
<tr>
<th width="5%" class="text-center">No.</th>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
@foreach($student_data as $student)
<tr>
<td width="5%"><?php echo $i; ?></td>
<td>{{ $student->name }}</td>
<td>{{ $student->description }}</td>
</tr>
<?php $i++; ?>
@endforeach
</tbody>
</table>
您認爲您可以直接調用關係的方法:
@foreach(auth()->user()->students as $student)
{{ $student->name }}
@endforeach
假設你的聯繫方法名是students
。