Laravel Blade: How to iterate a model's belongsToMany relationship in a table loop?
P粉106301763
P粉106301763 2024-03-31 22:30:44
0
2
385

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粉106301763
P粉106301763

reply all(2)
P粉768045522

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):



    @foreach($models as $model)
        
    @endforeach
    
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 view
P粉176203781

I create the table this way by checking the relationship:



            @foreach ($data['packages'] as $item)
                
            @endforeach
        
        @foreach ($data['tests'] as $item)
            
                @foreach ($data['packages'] as $pack)
                    @if($item->packages->contains($pack->id))
                        
                    @else
                        
                    @endif
                    
                @endforeach
            
        @endforeach
    
{{$item->title}}
{{$item->title}}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template