php - 拉维尔 4.2 : How to convert raw query to Eloquent

标签 php mysql laravel-4 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

我的 Controller 函数是

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 作为开始日期和结束日期,它会返回空字符串。

格式有问题吗? 我该如何解决这个问题?

最佳答案

最简单的方法是将 PHP 中的日期转换为数据库格式。

$start_date = date('Y-m-d', strtotime($start_date));

这应该导致您的数据库格式:2015-03-09

关于php - 拉维尔 4.2 : How to convert raw query to Eloquent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29009573/

相关文章:

php - 根据周、日选择 optgroup 匹配

php - 查询不能在 PHP 中运行,但可以直接在 MySQL 中运行

php - 某些表格后的分页符

php - Paypal IPN 调用 - fsockopen 超时

javascript - 重复从 SQL 服务器更新 HTML 表

php - Laravel Restful Controller ID

azure - 在 Azure 网站上强制使用 https - web.config - Laravel 4

php - 迭代 td 以从数据库中获取值

php - 将 IPV6 转换为 PTR 记录的半字节格式

php - mysql插入值选择不存在