刀片模板和单选按钮-在foreach循环中选择第一个 [英] Blade template and radio buttons - select first in foreach loop

查看:85
本文介绍了刀片模板和单选按钮-在foreach循环中选择第一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following Blade template entry, which creates (as part of a table row) a column of radio buttons.

我只想选择第一个广播电台,并且我想通过PHP进行此操作,没有js帖子页面加载.

I want to have only the first radio genereated selected, and I want to do this via the PHP, no js post page load.

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

How do I check if this is the "first" entry in my collection and thus place the string checked as an attribute in the HTML? My latest code permutation is below, but it does not work.

@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

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

More precisely, here's the line for the input:

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

我尝试了以下变化:

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

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

,甚至此建议显示为可行的模型我的问题:

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

谢谢,新年快乐!

推荐答案

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

If you did not specify keys for the elements of $organisationsOwned you can use the element's index to determine if it's the first one:

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

这篇关于刀片模板和单选按钮-在foreach循环中选择第一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆