正确的Laravel加入日期修复

时间:2018-11-15 05:04:50

标签: mysql laravel

我有两个数据表,一个是车辆,另一个是预订。我在预订车辆时从车辆表中选择要预订的车辆。现在,我想检查已预订和可用车辆的状态,但是当我以可用状态进行预订时,我正在获取车辆。原始预订表格如下。我正在更新已解决的代码。

$ date ='2018-11-14';

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<form action="" method="post" id="UserForm" class="form-horizontal">
<div class="modal-body">
    <div class="form-group">
        <label for="iUserName" class="col-sm-3 control-label">User Name</label>
        <div class="col-sm-9">
            <input type="text" class="form-control" name="iUserName" id="iUserName" placeholder="User Name" value="" />
        </div>
    </div>

    <div class="form-group">
        <label for="iPassword" class="col-sm-3 control-label">Password</label>
        <div class="col-sm-9">
            <input type="password" class="form-control" name="iPassword" id="iPassword" placeholder="iPassword" value="" />
        </div>
    </div>

    <div class="form-group">
        <label for="iConfPassword" class="col-sm-3 control-label">Confirm Password</label>
        <div class="col-sm-9">
            <input type="password" class="form-control" name="iConfPassword" id="iConfPassword" placeholder="Confirm Password" value="" />
        </div>
    </div>

    <div class="form-group">
        <label for="iRoleSelect" class="col-sm-3 control-label">Role</label>
        <div class="col-sm-9">
            <select class="form-control select2" id="iRoleSelect" name="iRoleSelect" style="width: 100%;"></select>
        </div>
    </div>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-info pull-left" data-dismiss="modal">Cancel</button>
    <button type="submit" class="btn btn-info pull-right">Save</button>
</div>
</form>

3 个答案:

答案 0 :(得分:0)

使用的右联接

$date = '2018-11-12';

  $data =DB::table('bookings')
              ->rightJoin('vehicles','vehicles.id','!=', 'bookings.vehicle_id')
              ->select('bookings.*', 'vehicles.*')
              ->where('bookings.event_date',$date)  //for date used where
              ->orWhereNull('bookings.event_date')   //if your bookings table is empty then add orWhereNull condition to get all rows...
              ->get();

答案 1 :(得分:0)

您需要拆分查询。

已预订:

$booked = DB::table('vehicles')
   ->select('bookings.*', 'vehicles.*')
   ->join('bookings', function($query) use ($date) {
        $query
            ->on('bookings.vehicle_id', '=', 'vehicles.id')
            ->where('date', '=', $date);
    })
    ->get();

可用:

$available = DB::table('vehicles')
    ->whereNotExists(function($query) use ($date) {
       $query
           ->select(DB::raw(1))
           ->from('bookings')
           ->whereRaw('bookings.vehicle_id = vehicle.id')
           ->where('bookings.event_date','=', $date);
    })
    ->get();

更改退货:

return view('bookings.status', compact('available', 'booked'));

在视图中使用$ available和$ booked代替一个数据变量。

OR

在一个查询中获取所有数据。

$data = DB::table('vehicles')
->select('bookings.*', 'vehicles.*')
->leftJoin('bookings', function($query) use ($data) {
    $query
        ->on('bookings.vehicle_id', '=', 'vehicle.id')
        ->where('date', '=', $date);
})
->get();

例如,查看视图中是否有空的'event_date'。

可用:

@foreach($data as $row)
    @if(!$row->event_date)
        ...table row...
    @endif
@endforeach

已预订:

@foreach($data as $row)
    @if($row->event_date)
        ...table row...
    @endif
@endforeach

答案 2 :(得分:0)

您应该尝试以下操作:

$data =DB::table('vehicles')
              ->join('bookings','bookings.vehicle_id', '!=', 'vehicles.id')
              ->select('bookings.*', 'vehicles.*')
              ->whereDate('bookings.event_date','=', $date)
              ->get();