I want to loop my packages in the table (each package has belongsToMany relationship with the test model) & I don't know how to loop in my blade?
public function tests() { return $this->belongsToMany('App\Models\Test'); }
Here is my complete form (blade):
<table> <thead> <tr> <th></th> <th><span>Child</span></th> <th>Women</th> <th>Men</th> <th>Athletes</th> <th>VIP</th> </tr> </thead> <tbody> <tr> <td class="title"><b>TestOne</b></td> <td><i class="fa-solid fa-check"></i></td> <td><i class="fa-solid fa-check"></i></td> <td></td> <td></td> <td></td> </tr> <tr> <td class="title"><b>TestTwo</b></td> <td></td> <td></td> <td><i class="fa-solid fa-check"></i></td> <td></td> <td><i class="fa-solid fa-check"></i></td> </tr> <tr> <td class="title"><b>TestThree</b></td> <td></td> <td><i class="fa-solid fa-check"></i></td> <td></td> <td></td> <td></td> </tr> <tr> <td class="title"><b>TestFour</b></td> <td><i class="fa-solid fa-check"></i></td> <td><i class="fa-solid fa-check"></i></td> <td></td> <td><i class="fa-solid fa-check"></i></td> <td></td> </tr> <tr> <td class="title"><b>TestFive</b></td> <td><i class="fa-solid fa-check"></i></td> <td><i class="fa-solid fa-check"></i></td> <td><i class="fa-solid fa-check"></i></td> <td><i class="fa-solid fa-check"></i></td> <td></td> <td></td> </tr> <tr> <td class="title"><b>TestSix</b></td> <td></td> <td><i class="fa-solid fa-check"></i></td> <td><i class="fa-solid fa-check"></i></td> <td><i class="fa-solid fa-check"></i></td> <td><i class="fa-solid fa-check"></i></td> </tr> </tbody> </table>
Each <i class="fa-solid fa-check">
Displays one row of the pivot table between
The design is:
How do I loop through relational models in my blde?
P粉7680455222024-04-01 20:50:27
If you have actually sent the list of models to the view, you can use blade directives like this (this is just a mock model, I don't know what your model actually looks like):
Child | Women | Men | Athletes | VIP | |
---|---|---|---|---|---|
{{ $model->title }} | @if ($model->is_child) @endif | @if ($model->is_women) @endif | @if ($model->is_men) @endif | @if ($model->is_athletes) @endif | @if ($model->is_vip) @endif |
@if condition is just my guess. Enter whatever you need to determine whether the fa check should be rendered.
Usually sending data is to view the following content:
$data['models'] = MyModel::all(); return view('myView', $data);
You can then access the variable $models
in the viewP粉1762037812024-04-01 12:21:53
I create the table this way by checking the relationship:
@foreach ($data['packages'] as $item) | {{$item->title}} | @endforeach|
---|---|---|
{{$item->title}} | @foreach ($data['packages'] as $pack) @if($item->packages->contains($pack->id))@else | @endif @endforeach |