Home  >  Q&A  >  body text

Laravel Blade: How to iterate a model's belongsToMany relationship in a table loop?

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粉106301763P粉106301763225 days ago372

reply all(2)I'll reply

  • P粉768045522

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

    
        @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

    reply
    0
  • P粉176203781

    P粉1762037812024-04-01 12:21:53

    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}}

    reply
    0
  • Cancelreply