我在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)。任何想法?
感谢您的帮助。
答案 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();