How to get first value returned by foreach

Asked

Viewed 231 times

0

Personal I am new in programming and I am trying to solve my problem with foreach...

I have a foreach that returns me the groups of photos that have photos in them... however I need to take the first group that foreach find photos and apply the attribute checked in his input tag... I tried to do with Empty but I’m not getting... Below is the code... Thanks! Hug!

@foreach($property_get->getPhotoGroupList() as $key => $grupo)
  <input id="tab{{ $key }}" type="radio" name="tabs" {{  !empty($key) ? 'checked'  :  ' ' }}>
  <label class="band band-two lastWord" for="tab{{ $key }}">{{ $grupo->getName() }}</label>@endforeach
  • Give a print_r($property_get->getPhotoGroupList()) and put the result here

  • Ele aparece um array aqui&#xA;&#xA;array:4 [▼&#xA; 3 => PhotoGroup {#391 ▶}&#xA; 1 => PhotoGroup {#394 ▶}&#xA; 2 => PhotoGroup {#406 ▶}&#xA; 4 => PhotoGroup {#409 ▶}&#xA;]

1 answer

1

check if the key is the first key if yes mark checked otherwise do not mark

@foreach($property_get->getPhotoGroupList() as $key => $grupo)
    <input id="tab{{ $key }}" type="radio" name="tabs" {{  @if($key == 0) ? 'checked'  :  ' ' }}>
    <label class="band band-two lastWord" for="tab{{ $key }}">{{ $grupo->getName() }}</label>
@endforeach
  • It is giving syntax error... probably because it is being done in Arabic right ?

  • Try adding a @ before if example @if($key == 0)

  • or remove {{ }}, I never used the frameword from the Blade

Browser other questions tagged

You are not signed in. Login or sign up in order to post.