简体   繁体   中英

nested foreach loop in laravel blade

I have nested foreach in my blade in laravel but all records compact twice in my select option here is my code:

@foreach($user as $user)
    @foreach($user_renter as $last_user_renter)
        @if($user->id == $last_user_renter->user_id)
           <option value="{{ $user->id }}" selected>
              {{ $user->name }} {{ $user->family }} - {{ $user->email }}
           </option>
        @else
           <option value="{{ $user->id }}">
              {{ $user->name }} {{ $user->family }} - {{ $user->email }}
           </option>
        @endif
    @endforeach
@endforeach

now is there any way to compact them once?

Try to change the user_renter as array as given bellow

$user_renter = array(
   array(
    'name' => 'flash',
    'id' => 1
),
array(
    'name' => 'zoom',
    'id' => 2
),
array(
    'name' => 'snart',
    'id' => 3
)
 );

And

@foreach($users as $user)
      <?php $key = array_search($user->id, array_column($user_renter, 'id'));  ?>
       <option value="{{ $user->id }}" @if(is_int($key)) selected @endif>
          {{ $user->name }} {{ $user->family }} - {{ $user->email }}
       </option>
@endforeach

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM