>我使用以下方法使用三元运算符比较 laravel 中 2 个不同表中的 2 个变量
@foreach ($creations as $creation)
@foreach ($usera as $user)
<div class="list-wrapper">
<div class="col-md-4">
<a class="btn btn-sm {{ $user->creation_id == $creation->id ? 'btn-primary btn-removeList' : 'btn-primary btn-addList' }}"
data-model-id="{{$creation->id}}">
{{$user->creation_id == $creation->id ? 'delete' : 'add'}}
</a>
</div>
</div>
@endforeach
@endforeach
我无法弄清楚这段代码有什么问题。谁能帮忙?
??
是一个空合并运算符。您正在将其与三元运算符的另一半一起使用,这是错误的。三元运算符的使用方式如下:
<a class="btn btn-sm {{ $user->creation_id == $creation->id ? 'btn-primary btn-removeList' : 'btn-primary btn-addList' }}"
data-model-id="{{$creation->id}}">
{{$user->creation_id == $creation->id ? 'delete' : 'add'}}
</a>
将??
更改为?
,您应该没问题。
你可以试试这个
<a class="btn btn-sm {{ ($user->creation_id == $creation->id) ? 'btn-primary btn-removeList' : 'btn-primary btn-addList' }}"data-model-id="{{$creation->id}}">
{{($user->creation_id == $creation->id) ? 'delete' : 'add'}}
</a>