I am trying access my model in my blade template. So I can loop thorough data.
I have tried calling global function like
{{ Auth::user }}
and it works fine. I can output user data on my view.
I have created a model called students which holds students data with user_id which is coming from user table. Like 1->N relationship. A user has multiple students associated with it. How can I call custom model in my view.
You can create a variable and pass it to the view:
$user = User::where('id', 1)->with('students')->first();
return view('some.view', compact('user'));
Then you can itearte over students in the view:
@foreach ($user->students as $student)
{{ $student->name }}
@endforeach
Pass student data to view
$students = Student::all();
return view('student_view')
->with('student_data', $students);
View like this in table
<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>
In your view your can directly call the relation method as:
@foreach(auth()->user()->students as $student)
{{ $student->name }}
@endforeach
Assuming your relation method name is students
.