sortByDesc() not Working as intended

Posted 5 months ago by SeroS

I have this 2 functions, the problem is that when i use the función sortByDesc, the new collection $clientes is not sorted as it should, if you need more info just ask me, thanks in advance.

public function estado_cc_clientes(){
      $clientes = cliente::select('id','nombre','codigo_empresa','condiciones_pago')->get();

      foreach ($clientes as $cliente) 

        $cliente->cc_final = $this->sacar_cc_final_cliente($cliente->id);

      $clientes = $clientes->sortByDesc('cc_final');

      return view('estadisticas.estado_cc_clientes',compact('clientes'));
public function sacar_cc_final_cliente($cliente_id){
      $cc_debe = cc_clientes::where('cliente_id',$cliente_id)->whereIn('tipo_de_documento',['fc','nd'])->where('borrado',0)->sum('importe');

      $cc_haber = cc_clientes::where('cliente_id',$cliente_id)->whereIn('tipo_de_documento',['rc','nc'])->where('borrado',0)->sum('importe');

      $cc_final = $cc_debe - $cc_haber;

      $cc_final = number_format($cc_final,2);

      return $cc_final;

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up


Reply to

Use Markdown with GitHub-flavored code blocks.