简体   繁体   中英

How to print the first object in the multidimensional array (forelse loop) in Laravel

I Want to Display just the first object (location in my case).

    @forelse($locations as $key => $value) 

I tried this:

<?php
     @forelse($locations as $key => $value) {
       $value = $locations->first();
    }
?>

it gives a syntax error, unexpected 'as' (T_AS), expecting ')'

    $result = $query->get();

    $locations = $result->pluck('name', 'id');

    $price_groups = SellingPriceGroup::forDropdown($business_id);

    if ($show_all) {
        $locations->prepend(__('report.all_locations'), '');
    }

ack.imgur.com/avYXn.png

You can use the $loop variable like. It should be:

@forelse ($locations as $key => $value)
  @if ($loop->first)
  //this is the first iteration.
  @endif
@empty
  //Logic when the array is empty
@endforelse

Check the doc: https://laravel.com/docs/8.x/blade#loops

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