Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PHPLaraDev's avatar

Problem with swiperjs when i make foreach for sliders it stretch first slider in blade?

Problem with swiperjs when i make foreach for sliders it stretch first slider then when load swiperjs it work perfect why this happen may be because the foreach work first before swiperjs execute the js code or css styles but i ensure assetst is work fine

       <div class="Swiper mySwiper "  init="true">
             <div class="swiper-wrapper">
                @foreach ($sliders ?? [] as $slider)
                    <div class="swiper-slide"  lazy="true">
                        <a href="{{ $slider['url'] }}">
                            <div class="slide swiper-lazy"  lazy="true"  style="background-image: url('{{ $slider['image'] }}')"  >
                                <div    loading="lazy" 
                                    style="background-image: url('{{ asset('landing-assets/images/header-slide-without.png') }}'); background-repeat: no-repeat; background-position: right 16%; padding: 2rem; height: 50%;">
                                    @if ($slider['category'])
                                        <h2> {{ $slider['category'] }} </h2>
                                    @endif
                                    <span> {{ $slider['author'] }} </span>
                                    <h3>
                                        <strong style="background: transparent; color: #fff; font-size: 30px;">
                                            {{ $slider['title'] }}
                                        </strong>
                                    </h3>
                                </div>
                            </div>
                        </a>
                    </div>
                @endforeach
            </div>
            <div class="swiper-button-prev"></div>
            <div class="swiper-button-next"></div>
        </div> 



     <script>
  	$(document).ready(function () {
    // Add animation class to Swiper container 
      // Swiper: Slider
new Swiper(".mySwiper", {
    loop: true,
    preloadImages: true, // تمكين التحميل المسبق
    lazy: true, // Enable lazy loading
    paginationClickable: true,
    breakpoints: {
        1250: {
            slidesPerView: 4,
        },
        768: {
            slidesPerView: 3,
        },
        480: {
            slidesPerView: 1,
        },
    },
    navigation: {
        nextEl: ".swiper-button-next",
        prevEl: ".swiper-button-prev",
    },
});

});

0 likes
4 replies
PHPLaraDev's avatar

problem is for every refresh the first slider take full width then swiperjs work and laod 4 slider but i want it to load the 4 slider not stretch the first. Can someone give me a hint ?

gych's avatar

This is normal because like you already mentioned the foreach is executed before the script. You could try to solve this by rendering the sliders in your script after Swiper is loaded.

PHPLaraDev's avatar

@gych can i use append or something using js code to load sliders is that best practice!

gych's avatar

@PHPLaraDev Yes you can try to use append or only show the specific slider elements after Swiper is loaded

Please or to participate in this conversation.