我使用PDO来获取上一周或一个月的项目,如下面的代码所示。现在我将该代码转换为Laravel。所以,我想知道是否有Eloquent方法来实现以下查询:
public function trendingAlbums($category=null,$order='views',$from=0,$limit=20,$term=null) {
$cond = isset($category) ? "AND category_id IN ($category)" : '';
if($term == 'month') {
$cond .= "AND created_at BETWEEN DATE_SUB(CURRENT_DATE() ,INTERVAL 1 MONTH) AND CURRENT_DATE()";
} elseif ($term =='week') {
$cond .= "AND created_at BETWEEN DATE_SUB(CURRENT_DATE() ,INTERVAL 7 DAY) AND CURRENT_DATE()";
}
$sql = "SELECT * FROM view_albums WHERE 1 $cond ORDER BY $order DESC LIMIT :from, $limit";
try {
$q = $this->con->prepare($sql);
isset($category) ? $q->bindValue(':category',$category,PDO::PARAM_STR) : '';
$q->bindvalue(':from',$from,PDO::PARAM_INT);
$q->execute();
} catch (PDOException $ex) {
return $this->errorhandler($ex->getmessage()); // displaying/hanlding actual error message
//return $ex->getMessage();
}
$r= $q->fetchAll(PDO::FETCH_OBJ);
return $r;
}
答案 0 :(得分:2)
使用碳。它允许您轻松创建日期,如Carbon :: today() - > subWeeks(1)或Carbon :: today() - > subMonths(1)。
答案 1 :(得分:2)
你可以这样做
connection.model.find({
'abc.sar' : {
$elemMatch : {'u_id': new ObjectID( '56e3e5b6fb33e53f119051f2' )}
}
});