Ty
2 months ago
113
18
Laravel

add a div every x number if items

Posted 2 months ago by Ty

I have data that is being output into the view and it is working great. But I need a way to group the items. I would like to be able to wrap every 4th item with a div. I have tried using chunk but have gotten errors.

my controller is as follows. Service listing is the one i'm using to get a list of service listings.

public function show($slug) {
        $serviceListings = ServiceListing::get();
        $services = Service::where('slug', $slug)->firstOrFail();

        return view('services.show')->with('services', $services)->with('serviceListings', $serviceListings);
}

my view looks like this. I can get chunk to work correctly but once I put the if statement in there to check for service category it tells service_category it not a instance of this collection.

<div class="servicesListingWrapper">
            <h3>Some of the services we offer.</h3>
            <div class="servicesListing">
            <div class="serviceListingColumn">
                <ul>
                @foreach($serviceListings as $serviceListing)
                @if ($serviceListing->service_category == $services->title)

                    <li class="serviceListingItem">{{$serviceListing->service_name}}</li>


                @endif
                @endforeach
            </ul>
        </div>
    </div>
        </div>

Please sign in or create an account to participate in this conversation.