簡體   English   中英

如何在Laravel中打印多維數組(forelse循環)中的第一個object

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

我只想顯示第一個 object(在我的例子中是位置)。

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

我試過這個:

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

它給出了一個語法錯誤,意外的 'as' (T_AS),期待 ')'

    $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

您可以使用 $loop 變量之類的。 它應該是:

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

檢查文檔: https://laravel.com/docs/8.x/blade#loops

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM