Blade 模板和单选按钮 - select 首先在 foreach 循环中

Blade template and radio buttons - select first in foreach loop

我有以下 Blade 模板条目,它创建(作为 table 行的一部分)一列单选按钮。

我只想选择生成的第一个广播,并且我想通过 PHP 执行此操作,无需加载 js post 页面。

如何检查这是否是我集合中的 "first" 条目,然后将字符串 checked 作为属性放入 HTML 中?我最新的代码排列如下,但它不起作用。

@foreach ($organisationsOwned as $organisationOwned)
    <tr class="organisations-table">
        <td><input type="radio" name="organisations" id="{!! $organisationOwned->id !!}" @if ($organisationsOwned{0}) {!! "checked" !!} @endif></td>
        <td>{!! $organisationOwned->org_title !!}</td>
        <td>{!! $organisationOwned->plan_title !!}</td>
        <td style="text-align: center;">{!! currency_format($organisationOwned->gst_amount) !!}</td>
        <td style="text-align: right;">{!! $organisationOwned->subscription_ends_at !!}</td>
    </tr>
@endforeach

更准确地说,这是输入行:

<input type="radio" name="organisations" id="{!! $organisationOwned->id !!}" @if ($organisationsOwned{0}) {!! "checked" !!} @endif>

我试过变体,例如:

{{ head($organisationsOwned) ? checked : '' }}>

{{ $organisationsOwned{0} ? checked : '' }}>

甚至 this suggestion shown as a working model 我的问题:

@if ($organisationOwned == reset($organisationsOwned)) checked @endif

谢谢,新年快乐!

如果您没有为 $organisationsOwned 的元素指定键,您可以使用元素的索引来确定它是否是第一个:

@foreach($organisationsOwned as $index => $organisationOwned)
  ...
  <td><input type="radio" name="organisations" id="{!! $organisationOwned->id !!}" @if (!$index) {!! "checked" !!} @endif></td>
  ...
@endforeach