Laravel 4.2:如何将原始查询转换为 Eloquent
Laravel 4.2: How to convert raw query to Eloquent
我有一个工作正常的原始查询。
$qry ="select date(created_at) as Date,count(id) as Value from performances where date_format(created_at,'%d-%m-%Y') >= '$start_date' and date_format(created_at,'%d-%m-%Y') <= '$to_date' group by Date order by Date desc ";
$stats = DB::select( DB::raw($qry) );
return json_encode($stats);
我想将其转换为 Eloquent
我的控制器功能是
public function postPerformanceDetails()
{
$start_date = Input::get('start_date');
$to_date = Input::get('to_date');
$start_date = date('Y-m-d',strtotime($start_date));
$to_date = date('Y-m-d',strtotime($to_date));
$stats = Performance::where('created_at', '>=', $start_date)
->where('created_at','<=',$to_date)
->groupBy('perf_date')
->orderBy('perf_date', 'DESC')
->remember(60)
->get([
DB::raw('Date(created_at) as perf_date'),
DB::raw('COUNT(id) as perf_count')
])
->toJSON();
return $stats
}
原始查询工作正常,但 eloquent 根据日期输入不工作。
我以这种格式输入数据 09-03-2015
在数据库中,格式为 2015-03-09
如果我们给 2015-03-09 作为 start_date 和 to_date 它 returns 空字符串。
格式有问题吗?
我该如何解决这个问题?
最简单的方法是将 PHP
中的日期转换为数据库格式。
$start_date = date('Y-m-d', strtotime($start_date));
这应该导致您的数据库格式:2015-03-09
。
我得到了@sleepless建议的答案。
这是代码。
public function postPerformanceDetails()
{
$event = Input::get('events');
$start_date = Input::get('start_date');
$to_date = Input::get('to_date');
$start_date = date('Y-m-d H:i:s',strtotime($start_date.'00:00:00'));
$to_date = date('Y-m-d H:i:s',strtotime($to_date.'23:59:59'));
$stats = Performance::where('created_at', '>=', $start_date)
->where('created_at','<=',$to_date)
->groupBy('perf_date')
->orderBy('perf_date', 'DESC')
->remember(60)
->get([
DB::raw('Date(created_at) as perf_date'),
DB::raw('COUNT(id) as perf_count')
])
->toJSON();
return $stats;
}
我有一个工作正常的原始查询。
$qry ="select date(created_at) as Date,count(id) as Value from performances where date_format(created_at,'%d-%m-%Y') >= '$start_date' and date_format(created_at,'%d-%m-%Y') <= '$to_date' group by Date order by Date desc ";
$stats = DB::select( DB::raw($qry) );
return json_encode($stats);
我想将其转换为 Eloquent
我的控制器功能是
public function postPerformanceDetails()
{
$start_date = Input::get('start_date');
$to_date = Input::get('to_date');
$start_date = date('Y-m-d',strtotime($start_date));
$to_date = date('Y-m-d',strtotime($to_date));
$stats = Performance::where('created_at', '>=', $start_date)
->where('created_at','<=',$to_date)
->groupBy('perf_date')
->orderBy('perf_date', 'DESC')
->remember(60)
->get([
DB::raw('Date(created_at) as perf_date'),
DB::raw('COUNT(id) as perf_count')
])
->toJSON();
return $stats
}
原始查询工作正常,但 eloquent 根据日期输入不工作。
我以这种格式输入数据 09-03-2015 在数据库中,格式为 2015-03-09
如果我们给 2015-03-09 作为 start_date 和 to_date 它 returns 空字符串。
格式有问题吗? 我该如何解决这个问题?
最简单的方法是将 PHP
中的日期转换为数据库格式。
$start_date = date('Y-m-d', strtotime($start_date));
这应该导致您的数据库格式:2015-03-09
。
我得到了@sleepless建议的答案。
这是代码。
public function postPerformanceDetails()
{
$event = Input::get('events');
$start_date = Input::get('start_date');
$to_date = Input::get('to_date');
$start_date = date('Y-m-d H:i:s',strtotime($start_date.'00:00:00'));
$to_date = date('Y-m-d H:i:s',strtotime($to_date.'23:59:59'));
$stats = Performance::where('created_at', '>=', $start_date)
->where('created_at','<=',$to_date)
->groupBy('perf_date')
->orderBy('perf_date', 'DESC')
->remember(60)
->get([
DB::raw('Date(created_at) as perf_date'),
DB::raw('COUNT(id) as perf_count')
])
->toJSON();
return $stats;
}