Home > Software engineering >  Laravel Blade foreach combine
Laravel Blade foreach combine

Time:06-21

I have two separate divs, one where the images are loaded and the other where the data is displayed.

<div >
    <div >
        <div >

            <!-- Slide Item -->
            <div >
                <div  style="background-image: url(include/assets/images/main-slider/1.jpg);">
                </div>
            </div>
                                 
        </div>
    </div>
</div>

<div >
    <div >AVANT</div>
    <div >
        <div >
            @if(count($listings) > 0)
                @foreach($listings as $listing)
                    <!-- Slide Item -->
                    <div >
                        <div >
                            <div >
                                <div >
                                    <h5>{{$listing->first_name}} {{$listing->last_name}}</h5>
                                    <h1><span>{{$listing->name}}</span> </h1>
                                    <div >{{$listing->property_description}}.</div>
                                    <div ><a href="#">View Listing</a></div>
                                </div>
                            </div>
                        </div>
                    </div>
                @endforeach
            @endif

        </div>
    </div>
</div>

So the first div will also be in a foreach loop, but I dont know how I can combine the two where they show the same related data.

CodePudding user response:

Use 2 foreach loops . It is the easiest and desired solution for you. or you can define two variable and concat the contents using one forloop and then display them later like

@php
$first='';
$second='';
@endphp
 @if(count($listings) > 0)
    @foreach($listings as $listing)
       @php
       $first.='first div content';
       $second.='second div content';
       @endphp
     @endforeach
   @endif
  <div >
        <div >
            <div >
                 {!!$first!!}                
            </div>
        </div>
    </div>
     <div >
        <div >AVANT</div>
        <div >
            <div >
              {!!$second!!}
      
            </div>
        </div>
    </div>

CodePudding user response:

<html>
<body>
    @section('sidebar')
        This is the master sidebar.
    @show

    <div >
        @yield('content')
    </div>
</body>

use for yield

  • Related