php - Laravel 4.2: Convert a SQL select statement to Eloquent statement -
i have following db::select statement:
db::select("select user_id, count(*) orders_count order_book ob session_id='".$session_id."' group user_id");
which result like:
[{ "user_id": 2, "orders_count": 6 }, { "user_id": 340, "orders_count": 83 }, { "user_id": 341, "orders_count": 88 }]
what equivalent statement using eloquent model?
this have far:
order::wheresession_id($session_id)->select('user_id','count(*)')->groupby('user_id')->get();
you can use this:
order::select('user_id', db::raw('count(*) orders_count')) ->where('session_id', $session_id) ->groupby('user_id') ->get();
you need use db::raw
when using count
in query's select, because otherwise quoted pdo.
Comments
Post a Comment