如何不在搜索框中显示某些值?

时间:2019-09-20 08:24:23

标签: php database laravel search

我需要知道如何基于数据库中的列使用display: none进行一些设置吗?

因此,我需要从表value="1"的列active的{​​{1}}中禁用带有news的项,但是我不知道如何在脚本中执行此操作。

以下是搜索脚本:

    else if(searchcategory == 'news')
            {
                get_values['search'] = search;
                get_values['news_category_url'] = $('#header_news_category_select').val();

                $.ajax(
                {
                    url: "{{ url('search-news-ajax') }}",
                    // data: {get_values:get_values},
                    type: 'GET',
                    data: {
                      '_token': $('input[name=_token]').val(),
                      'get_values': get_values,
                    },
                    success: function( response )
                    {

                        href = '{{ url("view-all-news") }}?category='+ get_values['news_category_url'] + '&search=' + search;


                        response +=
                            '<li class="col-md-12 col-xs-12">'+
                                '<a class="row" href="'+href+'" style="white-space:normal;padding-left:unset;padding-right:unset" id="redirect_url">'+
                                    '<div class="col-md-12" style="text-align:center;"><h5>Find more with '+
                                         search +
                                    '</h5></div>'+
                                '</a>'+
                            '</li>';
                            // console.log(html);

                             $('.search-item-list').html(response);
                             $(".search-item-list").show();
                             $(".removelist").show();
                    }

                });

            }

NewsController:

     public function searchNewsAjax(requ $request)
    {
        // return $request->all();
        $categoryID =  \App\Category::pluck('id');

        $news = News::with('category')->whereHas('user', function($query) {
                    $query->where('deleted_at', '=', null);
                })->whereIn('category_id',$categoryID);

        if ($user = Sentinel::check())
        {
            if(!$user->inRole('admins'))
            {
                $id = $user->id;
                $news = $news->Where(function($query1) use($id){
                        $query1->where('public','=', 1)->orWhereHas('user.contact', function ($query) use ($id) {
                            $query->where('user_id', '=', $id);
                            })->orWhere('user_id','=',$id);
                });
            }
        }
        else{
            $news = $news->where('public','=', 1);
        }

        $news = $news->where('subject', 'LIKE', '%'.$request['get_values']['search'].'%');

        if(!empty($request['get_values']['news_category_url']))
        {
            $category_id = \App\Category::where('category_url', '=' ,$request['get_values']['news_category_url'])->pluck('id');
            $news = $news->where('category_id', '=',$category_id);
        }

        $news = $news->orderBy('date', 'desc')->take(5)->get();

             $news_data = [];
            foreach ($news as $key => $value) {
                $news_data[$key] = $value;
                $news_data[$key]['subject_url'] = Slugify::slugify($value->subject);
            }
            // echo json_encode($news);
            $data['news'] = $news;
            return view('preview.news',$data);

    }

以下是视图:

    <!-- Demo Pages -->
                <li class="mega-menu-fullwidth search-box-drop " style="margin-top:6px;">
                    <div class="search-box">
                        <div class="input-group animated ">
                            <input style="height: 33px !important;" class="form-control" placeholder="Search for Jobs, Courses, Career planning and more…" type="text" id="global_search_text" name="search">
                            <span class="input-group-btn">
                                <button class="btn-u submit" type="button">Go</button>
                            </span>
                        </div>
                    </div>

                    <ul class="dropdown-menu col-md-12 col-xs-12 search-item-list" >

                        <!-- About Pages -->
                        <li class="category search-category" data-val="jobs">
                            <a href="javascript:void(0);">Jobs<br>jobs</a>
                        </li>
                        <!-- End About Pages -->

                        <!-- Service Pages -->
                        <li class="category search-category" data-val="courses">
                            <a href="javascript:void(0);">Courses<br>jobs</a>
                        </li>
                        <!-- End Service Pages -->

                        <!-- Job Pages -->
                        <li class="events search-category" data-val="events">
                            <a href="javascript:void(0);">Events<br>jobs</a>
                        </li>
                        <!-- End Job Pages -->

                        <!-- Pricing Tables -->
                        <li class="news search-category" data-val="news">
                            <a href="javascript:void(0);">News<br>jobs</a>
                        </li>
                        <!-- End Pricing Tables -->

                        <!-- Login and Registration -->
                        <li class="opinions search-category" data-val="opinions">
                            <a href="javascript:void(0);">Opinions</a>
                        </li>
                        <!-- End Login and Registration -->



                    </ul>

所以,这是我的示例,第一个记录在活动列的数据库值1中处于禁用状态,我不需要在搜索时显示此记录,而在活动表列的数据库中值1在数据库中处于活动状态 photo

我知道很难从控制器中做到这一点,但是我再也找不到了,这不是我的代码,我也不知道是否存在,因为这在我的标题布局中。只能通过脚本来知道是否可行。

2 个答案:

答案 0 :(得分:2)

尝试一下

$news = News::with('category')->whereHas('user', function($query) {
$query->where('deleted_at', '=', null);
})->whereIn('category_id',$categoryID)->where("value","!=",1);

答案 1 :(得分:0)

使用以下代码更改您的代码:

public function searchNewsAjax(requ $request)
        {
            // return $request->all();
            $categoryID =  \App\Category::pluck('id');

            $news = News::with('category')->whereHas('user', function($query) {
                        $query->where('deleted_at', '=', null);
                    })->whereIn('category_id',$categoryID)
                    ->where('active','<>',1) // notice this line.. this is what you need.
                    ;

            if ($user = Sentinel::check())
            {
                if(!$user->inRole('admins'))
                {
                    $id = $user->id;
                    $news = $news->Where(function($query1) use($id){
                            $query1->where('public','=', 1)->orWhereHas('user.contact', function ($query) use ($id) {
                                $query->where('user_id', '=', $id);
                                })->orWhere('user_id','=',$id);
                    });
                }
            }
            else{
                $news = $news->where('public','=', 1);
            }

            $news = $news->where('subject', 'LIKE', '%'.$request['get_values']['search'].'%');

            if(!empty($request['get_values']['news_category_url']))
            {
                $category_id = \App\Category::where('category_url', '=' ,$request['get_values']['news_category_url'])->pluck('id');
                $news = $news->where('category_id', '=',$category_id);
            }

            $news = $news->orderBy('date', 'desc')->take(5)->get();

                 $news_data = [];
                foreach ($news as $key => $value) {
                    $news_data[$key] = $value;
                    $news_data[$key]['subject_url'] = Slugify::slugify($value->subject);
                }
                // echo json_encode($news);
                $data['news'] = $news;
                return view('preview.news',$data);

        }