Tuesday, March 21, 2017

Eloquent Query Building

I would like to know how can i transform this query into eloquent. Im having some troubles displaying those "counts" fields.

$resultados = DB::select( DB::raw("
    Select * From (
    Select
        P.*,
        count(distinct PP.id) as countProduct,
        count(distinct C.id) as countCity,
        count(distinct CO.id) as countCountry,
        count(distinct CON.id) as countContinent
    From
        packages P
        Inner join citie_package CP
            on P.id = CP.package_id
        Inner join cities C
            on CP.city_id = C.id
        Inner Join countries CO
            on C.country_id = CO.id
        Inner join continents CON
            on CO.continent_id = CON.id
        Inner Join product_package PP
            on P.id = PP.package_id
    where
       $whereCondition
    group by
        P.id,
        P.name,
        P.image,
        P.days,
        P.operator_id,
        P.badge,
        P.price,
        P.brochure,
        P.lodging,
        P.itinerary,
        P.services,
        P.observations,
        P.offer,
        P.featured,
        P.validity,
        P.created_at,
        P.updated_at,
        P.invisible,
        P.operationdates
        ) as pepe
    where
      $subWhereCondition


"));

For now the query is working good, the problem im having is with Mysql while searching rows:

"SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.

Thats why im planning to built it again but on this time usin eloquent.

Thanks!!



via Tomas Bolognesi

Advertisement