2016-03-01 37 views
1

我在這樣的Laravel查詢生成器中有一個查詢。我想從特定年份獲取數據Unix時間格式不工作在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

您需要使用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(); 
+0

太謝謝你了 – Meera