samedi 12 mars 2016

How to get Last Month Items via Elequent

previously in pdo i used to use the below code the get the items from last one week or one month..now i am converting that code into laravel..so i want to know if there is any helper or alternate ways to get the last week items

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;
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire