为什么有结果时消息不会隐藏?

时间:2018-08-14 11:40:30

标签: php laravel

我有一个页面,其中有4个选择菜单,供用户根据会议类别,城市,价格和日期过滤会议。

但是,如果用户的选择不返回任何结果,则应显示一条消息“没有可搜索的结果”,并且该消息有效,该消息出现。但是存在一个问题,例如,用户执行了不返回任何结果的选择,则出现消息,但是然后,如果用户执行了另一个返回结果的选择,则结果出现了,但消息没有隐藏,消息仍然显示为:没有结果。

您知道为什么出现此消息吗?

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,
        ]);
    }
}

1 个答案:

答案 0 :(得分:1)

此分配将始终返回true。

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

尝试将视图条件更改为此:

@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

或者您也可以在视图中使用类似的内容,然后完全删除控制器中的会话闪存。

@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