Unix时间格式在Laravel where子句中不起作用

时间:2016-03-01 08:45:04

标签: laravel-5

我在Laravel查询构建器中有这样的查询。我想从特定年份获取数据

$query = blogs::where(array('blogs.status' => '1','blog_categories.status' => '1','categories.status' => '1','blogs.published' => '1'));

if (isset($year) and $year != NULL)$query->where("FROM_UNIXTIME(blogs.pubdate, '%Y')", '=', $year); 
    $result= $query->leftJoin('blog_categories', 'blog_categories.blogid', '=', 'blogs.blogid')->leftJoin('categories', 'blog_categories.catid', '=', 'categories.catid')->orderBy('blogs.publisheddate', 'desc')->count();

我收到错误'FROM_UNIXTIME'命令。这里我的'blogs.pubdate'采用时间戳格式(1456313400)。任何想法?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您需要使用raw expressions

$query = blogs::where([
    'blogs.status' => '1',
    'blog_categories.status' => '1',
    'categories.status' => '1',
    'blogs.published' => '1'
]);

if (isset($year) && $year != NULL) {
    $query->whereRaw("FROM_UNIXTIME(blogs.pubdate, '%Y') = {$year}"); 
}

$result= $query->leftJoin('blog_categories', 'blog_categories.blogid', '=', 'blogs.blogid')
    ->leftJoin('categories', 'blog_categories.catid', '=', 'categories.catid')
    ->orderBy('blogs.publisheddate', 'desc')
    ->count();