A
A
Alexander Koregin2020-07-16 16:26:44
Laravel
Alexander Koregin, 2020-07-16 16:26:44

How to correctly display an object in Laravel?

I have an object with data

5f1054835a5bb753025302.png
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


String $commercials = $commercials->building;

Contains the object that I showed in the screenshot above, but I have an error when outputting Tell me

5f10558b43e79460121252.png

, what's the problem?

And if you dump an object in a loop, it will give you this

5f105817b69cf663032852.png

Example

@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

1 answer(s)
J
jazzus, 2020-07-17
@dragonika8

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;
}

will close this problem. If there are similar errors on the links, then also check through if only in the blade syntax. And so everything needs to be rewritten. Remove requests from the template in the php tag and make normal connections using with, rather than making requests to the database at each iteration.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question