猿问

选中复选框时laravel非法运算符和值组合

当我通过 POST 方法发送我的表单时,我需要检查复选框是否被选中。在我的控制器中,我有 4 个 if 是我检查的地方,但是当我这样做时,我会出错


非法的运算符和值组合。



我的看法


{!! Form::open(['action' => 'PersonsController@raport', 'method' => 'POST']) !!}

    @csrf

    <div class="form-group">

        <label for="exampleFormControlFile1">Personel:</label>

        <input class="form-control form-control-sm" type="text" name="data" id="data" value="{{date('Y-m-01')}}" width="100%" readonly/>

        {!! $errors->first('data', '<strong><p style="color:red;" class="help-block">:message</p></strong>') !!}     

        <div class="form-check">

            <input class="form-check-input" name="all" type="checkbox" value="1" id="all">

            <label class="form-check-label" for="defaultCheck1">

                All

            </label>            

        </div>

    </div>

    <div class="form-group">

        <label for="exampleFormControlFile1">User </label>

        {!! Form::select('users', $users, null, ['class'=>'form-control form-control-sm', 'placeholder'=>'All users']) !!}

        {!! $errors->first('users', '<strong><p style="color:red;" class="help-block">:message</p></strong>') !!}

    </div>

    {{Form::submit('Raport', ['class'=>'btn btn-primary btn-sm'])}}

    {!! Form::close() !!}

和控制器


 public function raport(Request $request)

    {

       //... some code


        if($request->input('all') !== 'on' && empty($request->input('users')))

        {

           $raport = Users::where('data', '>=', $request->input('data'))

            ->orderBy('id_users', 'asc')->get();

        }

        elseif($request->input('all') === 'on' && !empty($request->input('users')))

        {

            $raport = Users::where('id_users', $request->input('users'))

              ->orderBy('id_users', 'asc')->get();

        }

}


胡子哥哥
浏览 155回答 3
3回答

慕姐4208626

我建议首先重构这个代码:public function raport(Request $request){&nbsp; &nbsp; //... some code&nbsp; &nbsp; if(!$request->has('all')){&nbsp; &nbsp; &nbsp; &nbsp; $date=date('Y-m-01');&nbsp; &nbsp; &nbsp; &nbsp; if(!$request->filled('users')){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query = Users::where('data', '>=', $date);&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query = Users::where('id_users', $request->input('users'))->where('data', '>=', $date);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; if(!$request->filled('users')){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query = Users::all();&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query = Users::where('id_users', $request->input('users'));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; $raport=$query->orderBy('id_users', 'asc')->get();&nbsp; &nbsp; //... another some code}它可能无法解决您的问题,但它更清楚。笔记:错误Illegal operator and value combination可能是由于空值和字段(例如日期时间)之间的比较如果您想确定请求中是否存在某个值且不为空,您可以使用该filled方法

慕尼黑的夜晚无繁华

你有这个价值&nbsp; &nbsp;<input class="form-check-input" name="all" type="checkbox" value="1" id="all">这意味着您发送值复选框 = 1如果您没有设置该值,那么它的默认值是 = on因此将整数与字符串进行比较导致您的错误在您的情况下,laravel 在 request 中有方法,因此您可以检查表单中的复选框是否已选中你可以像这样在你的代码中应用它&nbsp; &nbsp; if(!$request->has('all') && empty($request->input('users')))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;$raport = Users::where('data', '>=', $request->input('data'))&nbsp; &nbsp; &nbsp; &nbsp; ->orderBy('id_users', 'asc')->get();&nbsp; &nbsp; }&nbsp; &nbsp; elseif($request->has('all') && !empty($request->input('users')))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $raport = Users::where('id_users', $request->input('users'))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->orderBy('id_users', 'asc')->get();&nbsp; &nbsp; }&nbsp; &nbsp; elseif($request->has('all') && empty($request->input('users')))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $raport = Users::orderBy('id_users', 'asc')->get();&nbsp; &nbsp; }&nbsp; &nbsp; elseif(!$request->has('all') && !empty($request->input('users')))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $raport = Users::where('id_users', $request->input('users'))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;->where('data', '>=', $request->input('data'))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;->orderBy('id_users', 'asc')->get();&nbsp; &nbsp; }

杨__羊羊

我遇到了同样的问题,我发现我在变量中传递了空值。
随时随地看视频慕课网APP
我要回答