micheal
4 months ago

laravel 5.7 , where clause with multiple values

Posted 4 months ago by micheal

I'm trying to make a report for bus transport system, and the report's form has "Driver" and "Route" field. it works fine when I just select "Driver" or "Route", but there is something wrong when i select both "Driver and Route".

any assist would be really appreciate :)

This is Form Section 

<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>

This is Route section

Route::get('post_genral_report/{start?}/{end?}' , '[email protected]_genral_report')->name('abd');

This is Controller


    function post_genral_report(Request $request,$start = null , $end = null)
    {
    
              $data = DB::table('registration_tickets')
                    ->join('trips', 'trips.id', 'registration_tickets.trip_id')
                    ->join('buses', 'buses.id', 'trips.bus_id')
                    ->join('drivers', 'drivers.id', 'trips.driver_id')
                    ->join('routes', 'routes.id', 'trips.route_id')
                    ->join('provinces as p1', 'p1.id', 'routes.from')
                    ->join('provinces as p2', 'p2.id', 'routes.to')
                    ->select('registration_tickets.*', 'p1.name as from', 'p2.name as to', 'buses.type', 'buses.plate', 'drivers.name as d_name', 'drivers.lastname as lname');
                 if ($start and $end) {
          
                 
                $data->whereBetween('registration_tickets.date',  [$start , $end]);
 }             
          
              if ($request->driverid) {
                $data->where('trips.driver_id', $request->driverid);
              }

                  if ($request->routeid){
                $data->where('trips.route_id' , $request->routeid);
              
}
              if ($request->driverid and $request->routeid) {
                $data->where([
                     'trips.driver_id', '=' , $request->driverid,
                     'trips.route_id', '=' ,  $request->routeid
                ]);
              }
                 $data = $data->get();
              
                

        return Datatables::of($data)->make(true);
    }


Please sign in or create an account to participate in this conversation.