搜索不适用于laravel

时间:2016-10-01 10:58:00

标签: php mysql laravel search filter

我有一个项目,因为我想按行业,教育水平,经验领域进行搜索,但我不想按照我想要的方式行事。在选择行业教育水平之后,我有一个名为行业的下拉列表,经验区域出现在一个复选框中。选择框后,我想搜索并显示结果。我的搜索控制器是

public function searchProfessionals (Request $request)
    {
        $industry = $request->input('industry');
        $educationLevels = $request->input('educationLevels');
        $areasOfExperiences = $request->input('areasOfExperiences');
        $startPrice = $request->input('start_price');
        $endPrice = $request->input('end_price');
        $meetingType = $request->input('meeting_type');

        if ($meetingType == 1) {
            $type = [1, 1];
        } elseif ($meetingType == 2) {
            $type = [2, 2];
        } elseif ($meetingType === 3) {
            $type = [1, 2];
        } else {
            $type = [1, 2];
        }

        $userMeetings = array();
        $userRoles = array();
        $users = array();
        $showUsers = array();
        $userHourRates = array();

        $userIndus = UserIndustry::where('industry_id', $industry)->get();

      $userDegrees = UserDegree::where('degree_id', $educationLevels)->get();
        $userAreaOfExperiences = UserAreaOfExperience::where('area_of_experience_id', $areasOfExperiences)->get();

        if (!empty($userIndus)) {
            foreach ($userIndus as $userInd) {
                if (!empty($userDegrees)) {
                    foreach ($userDegrees as $userDegree) {
                        if (!empty($userAreaOfExperiences)) {
                            foreach ($userAreaOfExperiences as $userAreaOfExperience) {

                                $userHourRates[] = UserTime::where('user_id', $userAreaOfExperience->user_id)->whereBetween('hour_rate', [$startPrice, $endPrice])->first();
                            }

                        }
                            $userHourRates[] = UserTime::where('user_id', $userDegree->user_id)->whereBetween('hour_rate', [$startPrice, $endPrice])->first();

                    }
                }
                    $userHourRates[] = UserTime::where('user_id', $userInd->user_id)->whereBetween('hour_rate', [$startPrice, $endPrice])->first();
        }

我的刀片模板是

@extends ('frontends.layouts.app')
@section ('main')
    <div id="content-block" class="margin-top-140">
        <div class="container-fluid block custom-container">
            <h3 class="block-title">Browse Professionals</h3>
            <div class="block-subtitle"><span>Connect</span> <span>Learn</span> <span>Inspire</span> <span>Thrive</span> </div>
            <div class="row">
                {{--<form action="" method="post">--}}
                    <div class="col-md-2 left-feild">
                        <div class="margin-bottom-30">
                            <h3>Search Filters</h3>
                        </div>
                        @if(Auth::check() && Auth::user()->user_role->role_id == 1000000)
                            <div class="be-vidget">
                                <h3 class="letf-menu-article"> Looking To </h3>
                                <div class="creative_filds_block">
                                    <div class="radio">
                                        <label><input type="radio" name="lookingTo" onclick="lookingTo(2)"> Seek Advice</label>
                                    </div>
                                    <div class="radio">
                                        <label><input type="radio" name="lookingTo" onclick="lookingTo(3)"> Share Advice</label>
                                    </div>
                                </div>
                            </div>
                            <script>
                                function lookingTo(e) {
                                    $.ajax({
                                        type: 'POST',
                                        url: '{{ URL::to('looking/to') }}/' + e,
                                        success: function (result) {
                                            $('#browse_professionals').html(result);
                                        }
                                    });
                                }
                            </script>
                        @endif
                        <div class="be-vidget">
                            <h3 class="letf-menu-article">Professionals</h3>
                            <div class="-creative_filds_block">
                                <div class="fp-panel-wrap">
                                    <a class="toggle-btn active" onclick="open_industry()" href="#" data-target="#ul_industry" id="a_industry">Industry</a>
                                    <ul id="ul_industry" class="fp-panel open" style="overflow:scroll; height:200px;">
                                        @if(!empty($industries))
                                            @foreach($industries as $industry)
                                                <li value="{{ $industry->id }}"><a href="javascript:void(0)" onclick="industry_search({{ $industry->id }})">{{ (!empty($industry['name'])) ? $industry['name'] : '' }}</a> </li>
                                            @endforeach
                                        @endif
                                    </ul>
                                    <script>
                                        function industry_search(e) {
                                            $.ajax({
                                                type: 'POST',
                                                url: '{{ URL::to('professionals/industry') }}/' + e,
                                                success: function (result) {
                                                    $('#education_experience').html(result);
                                                    $('#a_industry').attr('class', 'toggle-btn');
                                                    $('#ul_industry').css('display', 'none');
                                                    $('#education_experience').css('display', 'block');
                                                    $('#eduLevel').css('display', 'block');
                                                    $('#areaExperience').css('display', 'block');

                                                }
                                            });
                                        }
                                        function open_industry() {
                                            $('#education_experience').css('display', 'block'); 
                                            $('#education_experience').css('display', 'block');
                                            $('#eduLevel').css('display', 'none');
                                            $('#areaExperience').css('display', 'none');
                                        }
                                    </script>
                                    <div id="education_experience" style="">
                                        <a id="a_education" class="toggle-btn" href="#" data-target="#eduLevel">Education Level</a>
                                        <ul id="eduLevel" class="no-link fp-panel" style="overflow:scroll; height:200px;">


                                            @if(!empty($degrees))
                                                @foreach($degrees as $degree)
                                                    <li value="{{ $degree->id }}">{{ (!empty($degree['name'])) ? $degree['name'] : '' }}</a> </li>
                                                @endforeach
                                            @endif

                                        </ul>
                                        <a id="a_experience" class="toggle-btn" href="#" data-target="#areaExperience">Areas of Experience</a>
                                        <ul id="areaExperience" class="no-link fp-panel" style="overflow:scroll; height:200px;">

                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="be-vidget">
                            <h3 class="letf-menu-article">Meeting Preferences</h3>
                            <label>Price</label>
                            <br> <br>
                            <input type="hidden" id="price_ranger" class="range-slider" value="23"/>


                            <label style="margin-top: 30px;">Type</label>
                            <div class="form-group">
                                <select class="form-input" id="meeting_type">
                                    <option value="3">All</option>
                                    <option value="1">Phone Meeting</option>
                                    <option value="2">Web Meeting</option>
                                </select>
                            </div>
                        </div>

                        <button id="search_professionals" type="button" class="btn color-2 size-2 btn-block hover-1">Apply Filter</button>
                        @if(Session::has('remove_filter'))
                            <button type="button" class="btn btn-danger size-2 btn-block hover-1">Remove Filter</button>
                        @endif

                        <script>
                            var industry = '';
                            $('#ul_industry li').click(function() {
                                industry = $(this).attr('value');
//                                console.log(industry);
                            });

                            $("#search_professionals").click(function () {
                                var degrees = new Array();
                                $('input[name="degrees_checkbox"]:checked').each(function() {
                                    degrees.push(this.value);
                                });

                                var experiences = new Array();
                                $('input[name="experiences_checkbox"]:checked').each(function() {
                                    experiences.push(this.value);
                                });

                                var price_ranger = $("#price_ranger").val();
                                var price_ranger_array = price_ranger.split(",");
                                var start_price = price_ranger_array[0];
                                var end_price = price_ranger_array[1];

                                var meeting_type = $("#meeting_type").val();

                                $.ajax({
                                    'type' : 'post',
                                    'url' : '{{ URL::to('search/professionals') }}',
                                    'data' : {
                                        'industry' : industry,
                                        'educationLevels' : degrees,
                                        'areasOfExperiences' : experiences,
                                        'start_price' : start_price,
                                        'end_price' : end_price,
                                        'meeting_type' : meeting_type
                                    },
                                    'success' : function (result) {
                                        console.log(result);
                                        $('#browse_professionals').html(result);
                                    }
                                });
                            });
                        </script>
                    </div>
                {{--</form>--}}

                <div class="col-md-10">
                    <div id="browse_professionals" class="row _post-container_">
                        @if (!empty($valid_providers))
                            @foreach ($valid_providers as $provider)

                                <div class="category-1 custom-column-5">
                                    <div class="be-post">
                                        <figure class="ratio-4-3 be-img-block-alt">
                                            <div class="ratio-inner" style="background-image: url('{{ !empty($provider->user_detail->avatar) ? URL::to($provider->user_detail->avatar) : '' }}')">
                                                <img src="{{ !empty($provider->user_detail->avatar) ? URL::to($provider->user_detail->avatar) : '' }}" alt="{{ !empty($provider->username) ? URL::to($provider->username) : '' }}">
                                            </div>
                                        </figure>
                                        <div class="be-post-title">{{ (!empty($provider->user_share->share)) ? str_limit($provider->user_share->share, 90) : '' }}</div>
                                        <div class="author-post">
                                            <span><a href="{{ !empty($provider->username) ? URL::to($provider->username) : '' }}">{{ !empty($provider->user_detail->first_name) ? $provider->user_detail->first_name : '' }} {{ !empty($provider->user_detail->last_name) ? $provider->user_detail->last_name : '' }}</a></span>
                                        </div>
                                        <span>{{ (!empty($provider->user_detail->credentials)) ? str_limit($provider->user_detail->credentials, 25) : '' }}</span>
                                        <div data-value="4" class="static-rating"></div>
                                        <div class="info-block clearfix">
                                            <a class="btn color-1 size-2 hover-1 pull-right" href="{{ !empty($provider->username) ? URL::to($provider->username) : '' }}">Contact</a>
                                            <h3 class="rate"> ${{ (!empty($provider->user_time->hour_rate)) ? $provider->user_time->hour_rate : '' }} /hr</h3>
                                        </div>
                                    </div>
                                </div>
                            @endforeach
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

请帮帮我。我很困难。抱歉我的英文不好

1 个答案:

答案 0 :(得分:0)

答案是

public function searchProfessionals(Request $request)
    {
        $industry = $request->input('industry');
        $educationLevels = $request->input('educationLevels');
        $areasOfExperiences = $request->input('areasOfExperiences');
        $startPrice = $request->input('start_price');
        $endPrice = $request->input('end_price');
        $meetingType = $request->input('meeting_type');
        $userDetails = array();
        $userMeetings = array();
        $userRoles = array();
        $users = array();
        $showUsers = array();
        $userHourRates = array();
        $userDegrees = array();
        $userIndustryArray = array();
        $userExperience = array();
        $userAr = array();
        $data['usersDegress'] = '';
        $data['usersExperience'] = '';
        $intersect_array = array();
        $data['userDetails'] = UserDetail::all();
        $data['userTime'] = UserTime::whereBetween('hour_rate',[$startPrice,$endPrice])->get();
        $userPriceArray = array();

            foreach ($data['userTime'] as $price) {
                foreach ($data['userDetails'] as $userDetail) {
                    if ($price->user_id == $userDetail->user_id) {
                        $userPriceArray[] = $userDetail;
                    }
                }
            }
            $newArray = array();
            $data['userMeeting'] = UserMeeting::where('meeting_id', $meetingType)->get();
            foreach ($userPriceArray as $price) {
                foreach ($data['userMeeting'] as $meet) {
                    if ($price->user_id == $meet->user_id) {
                        $newArray[] = $price;
                    }
                }
            }



        if(!empty($industry)) {
            $data['userIndustry'] = UserIndustry::where('industry_id',$industry)->get();
            foreach ($data['userIndustry'] as $userIn){
                foreach ($newArray as $new){
                    if($new->user_id == $userIn->user_id){
                        $userIndustryArray[] = $new ;
                    }
                }
            }
            if(count($educationLevels) >0){
                $data['usersDegress'] =  UserDegree::whereIn('degree_id',$educationLevels)->get();
                foreach ($data['usersDegress'] as $education ){
                    foreach ($userIndustryArray as $n){

                        if($n->user_id == $education->user_id){
                            $userDegrees[] = $n ;
                        }

                    }
                }
            }
            if(count($areasOfExperiences) >0){
                $data['usersExperience'] =  UserAreaOfExperience::whereIn('area_of_experience_id',$areasOfExperiences)->get();
                foreach ($data['usersExperience'] as $experience ){
                    foreach ($userIndustryArray as $ex){

                        if($ex->user_id == $experience->user_id){
                            $userExperience[] = $ex ;
                        }

                    }
                }
            }

        }else{
            $userIndustryArray = $newArray ;
        }
        if(count($educationLevels)> 0 && count($areasOfExperiences)  >0) {
            $intersect_array = array_intersect ($userDegrees,$userExperience);
                $userIndustryArray =$intersect_array;

        }else   if(count($educationLevels) == 0 && count($areasOfExperiences)  >0) {
            $intersect_array = $userExperience;
            $userIndustryArray =$intersect_array;

        }
        else   if(count($educationLevels)> 0 && count($areasOfExperiences)  == 0) {
            $intersect_array = $userDegrees;
            $userIndustryArray =$intersect_array;

        }else   if(count($educationLevels) == 0 && count($areasOfExperiences) == 0) {
            $userIndustryArray =$userIndustryArray;

        }
        $string = '' ;

        foreach ($userIndustryArray as $item){
            $userRatingShow = Review::where('provider_id',$item->user_id)->avg('rating');
            if(empty($userRatingShow)){
                $userRatingShow = 0;
            }
            $avatar = '';
            $name = '';
                $user = User::find($item->user_id) ;
            if(!empty($user)){
                $name = $user->username ;
            }
            if(!empty($item->avatar )){
                $avatar = url($item->avatar);
            }else{
            }
            $userTime= UserTime::where('user_id',$item->user_id)->orderby('id','desc')->first();

            if(!empty($userTime)){
                $userTimeShow = $userTime->hour_rate ;
            }
            $bio = '';
            $user_share = UserShare::where('user_id',$item->user_id)->orderby('id','desc')->first();
            if(!empty($user_share)) {
                $bio =str_limit($user_share->share,85);
            }

            $string .= '<div class="category-1 custom-column-5">
                    <div class="be-post">
                        <figure class="ratio-4-3 be-img-block-alt">
                            <div class="ratio-inner" style="background-image: url(' . $avatar . ')">
                                <img src="' . $avatar . '" alt="omg">
                            </div>
                        </figure>
                        <div class="be-post-title">' . $bio . '</div>
                        <div class="author-post">
                            <span>
                                <a href="' . url($name) . '">' .
                                      $item->first_name . ' ' . $item->last_name . '
                                </a>
                            </span>
                        </div>
                        <span>' . $item->credentials . '</span>
                    </div>
                    <div data-value="' . $userRatingShow . '" class="static-rating"></div>
                        <div class="info-block clearfix">
                            <a class="btn color-1 size-2 hover-1 pull-right" href="' . url($name) . '">Contact</a>
                            <h3 class="rate">$' . $userTimeShow . '/hr</h3>
                        </div>
                    </div>
                </div>
            ';

        }
        if(!empty($string)) {
            return response($string);
        }else{
            echo '<div class="text-center margin-top-140"><h2>Sorry, no members meet your criteria.</h2></div>';

            echo '<div  class="text-center margin-top-50"><h2>Please revise your search.</h2></div>';
        }


    }