无法通过laravel中的复选框检索数据

时间:2016-09-29 03:23:07

标签: php mysql laravel checkbox laravel-blade

我有一个项目,我希望通过复选框从数据库中检索值。但我不能这样做。 我的控制器代码是

public function browseProfessionals() {
        $userRoles = UserRole::whereIn('role_id', [2,3])->get();
        $users = array();
        $showUsers = array();

        foreach ($userRoles as $userRole) {
            $users[] = User::find($userRole->user_id);
        }

        $valid_providers = array();
        $all_providers = array();
        $user_role_all_providers = UserRole::where('role_id', 2)->get();

        foreach ($user_role_all_providers as $user_role_all_provider) {
            $all_providers[] = User::where('id', $user_role_all_provider->user_id)->first();
        }

        foreach ($all_providers as $all_provider) {
            $user = User::where('id', $all_provider->id)->first();
            $user_detail = UserDetail::where('user_id', $all_provider->id)->first();
            $user_meeting = UserMeeting::where('user_id', $all_provider->id)->first();
            $user_time = UserTime::where('user_id', $all_provider->id)->first();

            if (!empty($user_detail->avatar) && !empty($user_meeting->meeting_id) && !empty($user_time->hour_rate) && $user->is_active == 1 && $user->is_deleted == 0) {
                $valid_providers[] = $all_provider;
            }
        }

        $data = [
            'valid_providers' => $showUsers,
            'industries' => Industry::where('is_active', 1)->where('is_deleted', 0)->get(),
            'degrees' => UserDegree::where('degree_id', $user->degree_id)->get(),
        ];
        return view('frontends.browse-professionals', $data);
    }

我的刀片模板

@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'); // display none hobe na. collapse hoye jabe
//                                        $('#a_industry').attr('class', 'toggle-btn active');
//                                        $('#ul_industry').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');
                            });

                            $("#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

我的Searchcontroller是

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();
//        return response($industry);

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

        if (!empty($userIndus)) {
            foreach ($userIndus as $userInd) {
                $userHourRates = UserTime::where('user_id', $userInd->user_id)->whereBetween('hour_rate', [$startPrice, $endPrice])->first();
            }

//        $userHourRates = UserTime::whereBetween('hour_rate', [$startPrice, $endPrice])->get();
            if (!empty($userHourRates)) {
                foreach ($userHourRates as $userHourRate) {
                    $userMeetings[] = UserMeeting::whereIn('meeting_id', $type)->where('user_id', $userHourRate->user_id)->first();
                }
                if (!empty($userMeetings)) {

                    foreach ($userMeetings as $userMeeting) {
                        $userRoles[] = UserRole::where('user_id', $userMeeting['user_id'])->where('role_id', '!=', 1)->first();
                    }
                    if (!empty($userRoles)) {
                        foreach ($userRoles as $userRole) {
                            $users[] = User::find($userRole['user_id']);
                        }
                        if (!empty($users)) {
                            foreach ($users as $u) {
                                $user = User::find($u['id']);
                                $userDetail = UserDetail::where('user_id', $u['id'])->first();
                                $userShare = UserShare::where('user_id', $u['id'])->first();
                                $userMeeting = UserMeeting::where('user_id', $u['id'])->first();
                                $userWeek = UserWeek::where('user_id', $u['id'])->first();
                                $userTime = UserTime::where('user_id', $u['id'])->first();
                                if (
                                    $user['is_active'] == 1 && $user['is_deleted'] == 0 && !empty($userDetail['avatar']) && !empty($userDetail['first_name']) &&
                                    !empty($userDetail['last_name']) && !empty($userDetail['credentials']) && !empty($userShare['share']) &&
                                    !empty($userShare['seek']) && !empty($userMeeting['meeting_id']) && !empty($userWeek['week_id']) &&
                                    !empty($userTime['start_time']) && !empty($userTime['end_time']) && !empty($userTime['hour_rate'])
                                ) {
                                    $showUsers[] = $u;
                                }
                            }
                            if (!empty($showUsers)) {
                                $string = '';
                                foreach ($showUsers as $showUser) {
                                    $userShow = User::find($showUser->id);
                                    $userDetailShow = UserDetail::where('user_id', $showUser->id)->first();
                                    $userShareShow = UserShare::where('user_id', $showUser->id)->first();
                                    $userRatingShow = Review::where('provider_id', $showUser->id)->orWhere('seeker_id', $showUser->id)->avg('rating');
                                    $userTimeShow = UserTime::where('user_id', $showUser->id)->first();
                                    $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(' . url($userDetailShow->avatar) . ')">
                                                <img src="' . url($userDetailShow->avatar) . '" alt="omg">
                                            </div>
                                        </figure>
                                        <div class="be-post-title">' . str_limit($userShareShow->share, 90) . '</div>
                                        <div class="author-post">
                                            <span>
                                                <a href="' . url($userShow->username) . '">' .
                                        $userDetailShow->first_name . ' ' . $userDetailShow->last_name . '
                                                </a>
                                            </span>
                                        </div>
                                        <span>' . $userDetailShow->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($userShow->username) . '">Contact</a>
                                            <h3 class="rate">$' . $userTimeShow->hour_rate . '/hr</h3>
                                        </div>
                                    </div>
                                </div>
                            ';
                                }
                                return response($string);
                            } else {
                                return 'data not found.';
                            }
                        } else {
                            return 'data not found.';
                        }
                    } else {
                        return 'data not found.';
                    }

                } else {
                    return 'data not found.';
                }
            } else {
                return 'data not found.';
            }
        } else {
            return 'data not found.';
        }
    }

请帮助我如何通过复选框显示价值。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

尝试通过F12调试您的JavaScript,并注意是否有任何错误。 如果没有Javascript问题,您还可以检查网络选项卡上的ajax请求返回数据。