4
I have a requested table as the example below:
Id | Cliente | Status
1 | XPTO | Proposta
2 | ABCD | Proposta
3 | XPTO | Venceu
4 | XPTO | Perdeu
And I want to present a result like this:
Cliente | Status | Qtd
XPTO | Proposta | 02
XPTO | Venceu | 01
ABCD | Proposta | 01
I’ve made a few attempts and got nothing!
The most I got was the result below in Mysql Workbrench, but I could not play it in my code:
Status | Qtd
Proposta | 03
Venceu | 01
Code executed in Mysql Workbrench:
SELECT distinct (status), count(cliente) as qtd_cliente
FROM portal.pedidos
group by status;
And how to do this in Laravel? Below I’m putting the controller code:
class RelatorioController extends Controller
{
public function index ()
{
$relatorio = \App\Pedido::join('clientes', 'cliente', '=', 'nome')
->get();
$cliente = \App\Cliente::lists('nome');
return view ('relatorio.index',compact('relatorio','cliente'));
}
public function status()
{
//o código abaixo é exatamente onde tenho dúvida, por isto o mesmo não está correto!!! Estas foram tentativas mal sucedidas dos meus testes.
$status = \App\Pedido::distinct('status')
->groupby('cliente')
->get();
return view ('relatorio.status', compact ('status'));
}
}
Why Question Tagged Laravel?
– Wallace Maxters
If there are several customers who have made proposals, which one you want to display in the column
Cliente
of response?– Victor Stafusa
If the problem is on the PHP side post the problem code graft.
– Anthony Accioly
I don’t see the need to use distinct since you are already grouping in your group by status;
– Marco Souza
by chance, in
4 | XPTO | Perdeu
would not be4 | XPTO | Proposta
?– Marcelo Diniz
To give the question as resolved just press the V on the right answer side.
– Leonardo