Answer the question
In order to leave comments, you need to log in
How to correctly display an object in Laravel?
I have an object with data
And I display it in a loop
@foreach ($types as $type)
@php
$commercials = $type->commercials->sortByDesc('created_at')->first();
$commercials = $commercials->building;
@endphp
@foreach ($commercials as $commercial)
<div id="{{ $loop->iteration }}" class="tabs__nav-tab" style="{{ !$loop->first ? 'display: none;' : '' }}">
<div class="grid">
<div class="grid__col-6 grid__col_md_1 mb-3">
<div class="complexes__image">
<div class="complexes__image-first">
<a href="{{ route('buildings.show', ['slug' => $commercial->building->slug]) }}">
<img src="{{ Voyager::image($commercial->building->image_header) }}" alt="Изображение комплекса">
</a>
</div>
{{-- <div class="complexes__image-second">
@foreach ($commercial->building->stickers->sortByDesc('created_at') as $sticker)
<div class="complexes__sticker">
<img src="{{ Voyager::image(json_decode($sticker->img)[0]->download_link) }}" alt="{{ $sticker->title }}" class="complexes__sticker-icon">
<span class="complexes__sticker-block">{{ $sticker->title }}</span>
</div>
@endforeach
</div> --}}
</div>
<div class="complexes__name">
<a href="{{ route('buildings.show', ['slug' => $commercial->building->slug]) }}" class="h h3 mb-1">{{ $commercial->building->title }}</a>
<span class="text text_body complexes__name-text text_regular"><svg class="complexes__name-icon"><use xlink:href="#icon-point"/></svg>
{{ $commercial->building->city->title }}, {{ $commercial->building->area->title }}, <wbr>{{ $commercial->building->address }}
</span>
</div>
<div class="complexes__desc">
<div class="complexes__desc-block mb-3">
<span class="text text_body text_regular">Класс жилья</span>
<div class="complexes__separation"></div>
<span class="text text_body text_regular">{{ $commercial->building->grade }}</span>
</div>
<div class="complexes__desc-block mb-3">
<span class="text text_body text_regular">Этажность</span>
<div class="complexes__separation"></div>
<span class="text text_body text_regular">{{ $commercial->building->floors }} эт.</span>
</div>
<div class="complexes__desc-block mb-3">
<span class="text text_body text_regular">Квартиры</span>
<div class="complexes__separation"></div>
@if($commercial->building->sales)
<span class="text text_body text_regular text_disable">нет в продаже</span>
@else
<span class="text text_body text_regular">от {{ getPrice($commercial->building->min_price) }}</span>
@endif
</div>
<div class="complexes__desc-block mb-3">
<span class="text text_body text_regular">Коммерция</span>
<div class="complexes__separation"></div>
@php
$commecial = $commercial->building->commercials->first();
@endphp
@if ($commecial)
<span class="text text_body text_regular">
от {{ getPrice($commecial->price) }}/м²
@if($commecial->commercial_type_id == 2)
(аренда)
@endif
</span>
@else
<span class="text text_body text_regular text_disable">нет в продаже</span>
@endif
</div>
</div>
<div class="complexes__buttons">
<a class="btn btn_primary btn_primary_big" href="{{ route('buildings.show', ['slug' => $commercial->building->slug]) }}">Узнать подробнее</a>
<a class="btn btn_secondary popup" data-title="Консультация по {{ $commercial->building->title }}" data-form-title="Консультация - {{ $commercial->building->title }}" href="#modal">Консультация</a>
</div>
</div>
</div>
</div>
@endforeach
@endforeach
$commercials = $commercials->building;
@foreach ($types as $type)
@php
$commercials = $type->commercials->sortByDesc('created_at')->first();
$commercials = $commercials->building;
@endphp
@foreach ($commercials as $commercial)
@dump($commercial)
@endforeach
@endforeach
Answer the question
In order to leave comments, you need to log in
The debug inside the loop will show the first object, not the problematic one. Error maybe one of the types has no commercials
Type crutch
if ($commercials) {
$commercials = $commercials->building;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question