Majid Alaeinia:
Tengo esta consulta en bruto:
SELECT business_id, count(*) as how_many from dbname.gateway_transactions WHERE DATE(created_at) = '2020-02-02' group by business_id order by how_many desc limit 5
Y este es el resultado:
Quiero tener el resultado de esta consulta (el contenido de la imagen), como una matriz. ¿Cómo puedo hacer esto?
He utilizado este:
$a = GatewayTransaction::selectRaw("SELECT business_id, count(*) as how_many from dbname.gateway_transactions WHERE DATE(created_at) = '2020-02-02' group by business_id order by how_many desc limit 5");
Sin embargo, no es capaz de conseguir lo que necesito. Necesito una gama tan amplia como la imagen de arriba:
[
51 => 725,
176 => 405,
229 => 264,
98 => 109,
136 => 92
]
Tsakhog:
Es necesario utilizar el constructor elocuente de esta manera:
$a = GatewayTransaction::selectRaw('business_id, count(*) as how_many')
->whereDate('created_at', '2020-02-02')
->groupBy('business_id')
->orderBy('how_many', 'desc')
->take(5)
->get();