Heim > Fragen und Antworten > Hauptteil
Ich möchte meine Pakete in der Tabelle schleifen (jedes Paket hat eine GehörtToMany-Beziehung zum Testmodell) und weiß nicht, wie ich mein Blade einschleifen soll?
public function tests() { return $this->belongsToMany('App\Models\Test'); }
Hier ist meine vollständige Form (Klinge):
<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>
Jedes <i class="fa-solid fa-check">
zeigt eine Zeile der Pivot-Tabelle dazwischen an
Das Design ist:
Wie durchlaufe ich relationale Modelle in meinem Blde?
P粉7680455222024-04-01 20:50:27
如果您实际上已将模型列表发送到视图,则可以使用 blade像这样的指令(这只是一个模拟模型,我不知道你的模型实际上是什么样子):
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 条件只是我的猜测。输入您需要的任何内容来确定是否应该渲染 fa 检查。
通常发送数据是为了查看如下内容:
$data['models'] = MyModel::all(); return view('myView', $data);
然后您可以在视图中访问变量 $models
P粉1762037812024-04-01 12:21:53
我通过检查关系以这种方式创建表:
@foreach ($data['packages'] as $item) | {{$item->title}} | @endforeach|
---|---|---|
{{$item->title}} | @foreach ($data['packages'] as $pack) @if($item->packages->contains($pack->id))@else | @endif @endforeach |