我需要从数据库中获取数据,条件是..在我的刀片中我已经给出了三个用于过滤的东西。一个是下拉列表,从那里将获取deviceID,然后是FromDate(Fdate)和ToDate(Tdate)。我需要获取数据,这些数据来自特定的deviceID以及这两个时间间隔之间的数据。 我怎样才能在laravel 5.2中编写sql查询? 我的表名是事件数据。
我的vehicle.blade.php这里给出了过滤选项
@extends('app')
</br></br></br></br></br></br></br></br>
@section('content')
<div class="templatemo-content">
<ol class="breadcrumb">
<li><a href="{{ url('/') }}"><font color="green">Home</font></a></li>
<li class="active">Vehicle Detail</li>
</ol>
<h1>Vehicle Detail</h1>
<p class="margin-bottom-15"></p>
<div class="row">
<div class="col-md-12">
@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form class="templatemo-preferences-form" role="form" method="POST" action="{{ url('/reports/vehicleReport/') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="row">
<div class="col-md-3 margin-bottom-15">
<label class="control-label">Vehicle ID</label>
<select class="form-control" value="{{ old('deviceID') }}" name="deviceID" >
@foreach( $vehicles as $vehicle)
@if ($vehicle->deviceID == old('description'))
<option value="{{ $vehicle->deviceID }}" selected>{{ $vehicle->description }}</option>
@else
<option value="{{ $vehicle->deviceID }}" >{{ $vehicle->description }}</option>
@endif
@endforeach
</select>
</div>
</div>
<div class="row">
<div class="col-md-3 margin-bottom-15">
<label class="control-label">From Date</label>
<input type="date" class="form-control" name="Fdate" value="">
</div>
<div class="col-md-3 margin-bottom-15">
<label class="control-label">To Date</label>
<input type="date" class="form-control" name="Tdate" value="">
</div>
</div>
</br></br></br></br>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-warning">
Get Report
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</br></br></br></br>
@endsection
Controller Page ReportController.php
<?php
namespace App\Http\Controllers;
use Mail;
use Illuminate\Support\Facades\DB;
use Faker\Provider\DateTime;
use App\User;
use App\Account;
use App\Device;
use App\Report;
use App\Http\Requests\createUserRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
use Symfony\Component\HttpFoundation\Request;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class ReportController extends Controller
{
public $type = 'Device';
public $types = 'Report';
public function getAdd()
{
$vehicles = DB::table('device')->get();
return view('reports.vehicleDetail')->with('vehicles', $vehicles);
// $cities = $this->getCities();
// $types = $this->getVehicleTypes();
// return view('pricing.add')->with(['data'=>['cities' => $cities,'types'=>$types]]);
}
public function Vreport(Request $request)
{
try {
$devices=DB::table('eventdata')-get()
->where("deviceID",$request['deviceID'] and timestamp between ($request['Fdate'] and $request['Tdate']));
// $devices = DB::table('eventdata')->get();
return view('vehicle.vehicleAdmin')->with('devices', $devices);
} catch (ModelNotFoundException $err) {
//Show error page
}
}
}
Model Report.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\Group as Authenticatable;
use Illuminate\Illuminate\Pagination\Paginator;
class Report extends Model
{
protected $table = 'eventdata';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'accountID'
,'deviceID'
,'timestamp'
,'statusCode'
,'latitude'
,'longitude'
,'gpsAge'
,'speedKPH'
,'heading'
,'altitude'
,'transportID'
,'inputMask'
,'outputMask'
,'address'
,'dataSource'
,'rawData'
,'distanceKM'
,'geozoneIndex'
,'geozoneID'
,'creationTime'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
任何人请帮忙.. 回应很明显..
答案 0 :(得分:1)
您可以使用以下查询:
$device_id = $request['deviceID'];
$from = $request['Fdate'];
$to = $request['Tdate'];
$devices = Report::where('deviceID',$device_id)
->whereBetween('timestamp',[$from,$to])
->get();
type="date"
只会提供日期和时间
资料来源:W3Schools
日期:定义日期控件(年,月,日(无时间))
datetime :已从HTML中删除输入类型datetime 标准。请改用datetime-local。
datetime-local :定义日期 和时间控制(年,月,日,小时,分钟,秒和分数 一秒钟(没有时区)
建议:您应该使用jQuery Date and Time picker轻松获取日期或时间。