user10066925
user10066925

Reputation:

Why the message dont hide when there are results?

I have a page that has 4 select menus for the user filter conferences based on the conference category, city, price and date.

But if the selection of the user dont return any result should appear a message "There are no results for your search", and it works , the message appear. However there is an issue, for example the user does a selection that dont return any result, the message appears, but then if the user do another selection that returns results, the results appear but the message dont hide, the message still appears saying that there are no results.

Do you know why the message appear?

HTML:

@if($flash = session('no_conferences'))
        <div class="alert alert-danger" role="alert">
            <strong><i class="fa fa-check-circle" aria-hidden="true"></i></strong>
            {{ $flash }}
        </div>
    @endif

    <form action="{{ route('search.index') }}" method="get">
        {{csrf_field()}}
        <div class="row">


            <div class="col-6 col-md-3 mb-4 mb-md-0">
                <select class="custom-select form-control font-size-xsm text-gray searchSelect"
                        name="category_id">
                    <option selected="" value="">All categories</option>
                    @foreach ($categories as $category)
                        <option value="{{ $category->id }}"
                                {{ (!empty($categoryId) && $categoryId == $category->id) ? 'selected' : '' }}>
                            {{ $category->name }}
                        </option>
                    @endforeach
                </select>

            </div>

            <div class="col-6 col-md-3 mb-4 mb-md-0">

                <select class="custom-select form-control"
                        name="city">
                    <option selected="" value="">Country</option>
                    @foreach($cities as $city)
                        <option value="{{ $city }}" {{ (!empty($selectedCity) && $selectedCity == $city) ? 'selected' : '' }}>
                            {{ $city }}
                        </option>
                    @endforeach
                </select>

            </div>

            <div class="col-6 col-md-3">
                <select class="custom-select form-control font-size-xsm"
                        name="price">
                    <option value="paid" {{ (!empty($price) && $price == 'paid') ? 'selected' : '' }}>
                        Paid
                    </option>
                    <option value="free" {{ (!empty($price) && $price == 'free') ? 'selected' : '' }}>
                        Free
                    </option>
                </select>
            </div>

            <div class="col-6 col-md-3">

                <select class="custom-select form-control"
                        name="range">
                    <option value="day" {{ (!empty($range) && $range == 'day') ? 'selected' : '' }}>
                        Tody
                    </option>
                    <option value="week" {{ (!empty($range) && $range == 'week') ? 'selected' : '' }}>
                        This Week
                    </option>
                </select>
            </div>
        </div>
    </form>

SearchController

class SearchController extends Controller
{
    public function index(Request $request){
        $categoryId = $request->get('category_id');
        $city       = $request->get('city');
        $price      = $request->get('price');
        $range      = $request->get('range');
        $pageLimit  = 8;

        $conference = Conference::Query()->where('status', 'P');


        if(!empty($categoryId)) {
            $conference = $conference->whereHas('categories', function($qry) use($categoryId) {
                $qry->where('category_id', $categoryId);
            });
        }

        if(!empty($city)) {
            $conference = $conference->where('city', $city);
        }

        if(!empty($price)) {
            if($price == "free") {
                $conference = $conference->whereHas('registrationTypes', function($qry)  {
                    $qry->where('price', '=', 0);
                });
            } elseif($price == "paid") {
                $conference = $conference->whereHas('registrationTypes', function($qry)  {
                    $qry->where('price', '>', 0);
                            });
                        }

        }


        if(!empty($range)) {
            if($range == "week") {
                $conference = $conference->where('start_date', '<', Carbon::now()->addDays(7));
            } 
            elseif($range == "day") {
                $conference = $conference->whereDate('start_date', '=', Carbon::today()->format('Y-m-d'));

            }
        }

        $conference = $conference->orderBy('created_at','desc')->paginate($pageLimit);

        if(count($conference) == 0){
            Session::flash('no_conferences', 'There are no results for your search  
');
        }

        return view('search', [
            'categories'     => Category::orderBy('created_at', 'desc')->get(),
            'cities'         => Conference::orderBy('created_at','desc')->pluck("city")->unique(),
            'conferences'     => $conference,
            'categoryId'     => $categoryId,
            'selectedCity'   => $city,
            'price'          => $price,
            'range'          => $range,
        ]);
    }
}

Upvotes: 0

Views: 57

Answers (1)

CUGreen
CUGreen

Reputation: 3186

This assignment will always return true.

@if($flash = session('no_conferences'))

Try changing your view condition to this:

@if(Session::has('no_conferences'))
    <div class="alert alert-danger" role="alert">
        <strong><i class="fa fa-check-circle" aria-hidden="true"></i></strong>
        {{ Session::get('no_conferences') }}
    </div>
@endif

Or you could just use something like this in your view and remove the session flash in your controller altogether.

@unless (count($conferences))   
    <div class="alert alert-danger" role="alert">
        <strong><i class="fa fa-check-circle" aria-hidden="true"></i></strong>
        There are no results for your search
    </div>
@endunless

Upvotes: 1

Related Questions