Laravel 5.7中具有多个值的Where子句

时间:2019-06-25 04:14:24

标签: php mysql ajax laravel laravel-5.7

我正在尝试为公交系统创建报告,该报告的表单包含“驱动程序”和“路线”字段。当我选择“驱动程序”以获取报告或仅选择“路由”以获取报告时,它工作正常,但是当我同时选择“驱动程序和路由”时,出现了问题。任何帮助将不胜感激

查看/表单

const data = [
{ID: 0, LABEL: 'turbo'},
{ID: 1, LABEL: 'classic'},
{ID: 7, LABEL: 'unknown'}
];

const hashMap = data.reduce((result, item) => {
  return { ...result, [ item.ID ] : item.LABEL };
}, {});

const hashMapJson = JSON.stringify(hashMap);

console.log('hashMap', hashMap);
console.log('hashMapJson', hashMapJson);
 
/*
More concise syntax:
console.log(data.reduce((result, { ID, LABEL }) => ({ ...result, [ ID ] : LABEL }), {}))
*/

路线

<form action="" method="get">
    <div class="row">
        <div class="col-md-3 form-group">
            <label>From Date:</label>
            <input type="date" name="start" class="form-control">
        </div>
        <div class="col-md-3 form-group">
            <label>To Date:</label>
            <input type="date" name="end" class="form-control">
        </div>
    </div>
    <div class="row">
        <div class="col-md-3" id="route_content">
            <label>Route</label>
            <select name="routeid" id="route_id" class="js-example-placeholder-singleuserid js-states form-control"
                    style="width: 100%; height:40px;">
                <option></option>
                <?php foreach ($routes as $key => $value): ?>
                <option value="{{$value->id}}">{{$value->from}} -> {{$value->to}}</option>
                <?php endforeach ?>
            </select>
        </div>
        <div class="col-md-3" id="driver_content" z>
            <label>Driver</label>
            <select name="driverid" id="driver_id" class="js-example-placeholder-single js-states form-control"
                    style="width: 100%; height:40px;">
                <option></option>
                <?php foreach ($driver as $key => $value): ?>
                <option value="{{$value->id}}"> {{$value->name}} </option>
                <?php endforeach ?>
            </select>
        </div>
    </div>
    <div class="row">
        <div class="col-md-3 form-group">
            <button class="btn btn-info btn-md" style="margin-top: 27px;">Search</button>
        </div>
    </div>
</form>

控制器

 Route::get('post_genral_report/{start?}/{end?}' , 
'reportController@post_genral_report')->name('abd');

1 个答案:

答案 0 :(得分:2)

  

您需要将同时使用driver_id和route_id的部分修改为实现您的目的(因为根据您的代码,当route_id或driver_id都在开始日期和结束日期之间时,似乎您想要结果,但是您的代码会分别检查driver_id和route_id的where条件。您需要将它们包装在where中,并在其中使用嵌套的where):

if ($request->driverid and $request->routeid) {
    $data->where(function($q) use ($request)
    {
        $q->where('trips.driver_id', '=', $request->driverid)
        ->where('trips.route_id', '=', $request->routeid);
    });
}