Laravel | Problem with the conditional loop in the search engine

Posted 4 months ago by ziben69

Hello guys,

I have a small problem over which I sit all day without effect. I have a piece of code:

      <div class="row">
            @foreach($results as $type => $list)
            @if (count($list) > 0)
            @foreach($list as $item)

            <div class="col-md-4" style="margin-bottom:10px;">
                <div class="card search-card">
                    <div class="card-body">
                        <strong class="card-title">
                            {{ $item->title}}
                        {!! $item->description !!}
            <h6>No data.</h6>
            <small>Try again.</small>

More specifically, I mean a conditional loop, which checks if the list contains more than 0 elements.

@if (count($list) > 0)

By searching in the database, it works, but if the data is missing, I just have a blank white screen. The message "no data" should appear.

I tried a lot of combinations and still nothing. I tried to adjust the loop before foreach. I tried: sizeOf, Count,! Empty, $ list! == [] etc. It still does not work. Please help.

Here is my controller:

    public function search(Request $request)
        $search = $request->get('search');
        $results = [];
        $results['calendar'] = Calendar::search($search)->paginate(5);
        $results['zone'] = Zone::search($search)->paginate(5);
        $results['tourism'] = Tourism::search($search)->paginate(5);
        $results['business'] = Business::search($search)->paginate(5);
        $results['culture'] = Culture::search($search)->paginate(5);
        $results['education'] = Education::search($search)->paginate(5);
        $results['inhabitant'] = Inhabitant::search($search)->paginate(5);
        $results['investor'] = Investor::search($search)->paginate(5);
        $results['senior'] = Senior::search($search)->paginate(5);
        $results['sport'] = Sport::search($search)->paginate(5);
        return view('pages.search', ['results' => $results]);

and search.blade.php

        <div class="row">
            <div class="col-12 col-lg-4">
                @foreach($results as $type => $list)
                    @include('pages.search-results.'. $type, ['items' => $list])

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

Reply to

Use Markdown with GitHub-flavored code blocks.