Home > Blockchain >  When doing two queries and a merge in Laravel 8, it doesn't sort by expiration date if i use so
When doing two queries and a merge in Laravel 8, it doesn't sort by expiration date if i use so

Time:03-09

I have made 2 queries to two different tables with with(), and then I have joined them with a merge. But I also want them to be displayed in order of expiration date.

As I have done the orderBy, it is not working for me. what will i be doing wrong? I would appreciate any help. Thank you and sorry for my english :(

Controller:

    public function listado_pendientes(Request $request)
  {
    $incio = Carbon::parse($request->fecha_inicio);
    $fin = Carbon::parse($request->fecha_fin);

    $datecheck =  $incio > $fin ? $fin : $incio;
    
    $dateStart = $datecheck && $datecheck < now() ? $datecheck : now();   
    $dateEnd = $fin && $fin < now() ? $fin : now();   

    // cargo relación de cobros con clientes según las fechas buscadas
    $cobros = Cobro::with('Contrato')
      ->whereBetween('vencimientos', [$dateStart,$dateEnd])     
      ->get();

    // cargo relación de facturas con clientes, según fechas buscadas y que no hayan sido cobradas
    $facturas = Factura::with('Contrato')
      ->whereBetween('vencimientos', [$dateStart,$dateEnd])
      ->where('cobrado', '=', NULL)      
      ->get();
    //dd($cobros);              
    $data = []; 
    $data = $cobros->merge($facturas);
    
    $data = collect($data)->sortByDesc('vencimientos');       

    return Voyager::view('voyager::custom.listado_pendientes', compact('data', 'dateStart','dateEnd'));
  }

Blade

 <tbody>
                                @foreach ($data as $cobro)
                                    <tr>                                                        
                                        <td>{{ $cobro->Contrato ? $cobro->Contrato->cliente : '' }}</td>
                                        <td>{{ $cobro->Contrato ? $cobro->Contrato->codigo : ' '}}</td>
                                        <td>{{ $cobro->Contrato ? $cobro->Contrato->full_address : ''}}</td>
                                        <td>{{ \Carbon\Carbon::parse($cobro->vencimientos)->format('d-m-Y') }}</td>
                                        <td>{{ $cobro->factura ? 'Factura ' : 'Cobro ' }}</td>
                                        <td>{{ $cobro->factura ? $cobro->factura : $cobro->id }}</td>
                                        <td>{{ $cobro->total }}</td>
                                    </tr>
                                @endforeach
                            </tbody>

dd, look the field "vencimientos"

 array:10 [▼
  6 => array:25 [▼
    "id" => 81185
    "idcliente" => 12421
    "factura" => 40621
    "fecha" => "2022-02-21"
    "baseimponible" => "23.00"
    "iva" => "4.83"
    "idiva" => 5
    "total" => "27.83"
    "vencimientos" => "2022-03-09"
    "domiciliaciones" => null
    "ccc" => null
    "observaciones" => null
    "autonomo" => null
    "razonsocial" => null
    "conceptos" => "POR SERVICIOS DE DESINSECTACION Y DESRATIZACION SEGÚN CONTRATO"
    "importes" => null
    "codigocliente" => null
    "formapago" => null
    "estado" => null
    "plazo" => "1"
    "tipopagos_id" => null
    "cobrado" => null
    "emailed" => null
    "remesada" => null
    "contrato" => array:76 [▶]
  ]
  9 => array:25 [▼
    "id" => 81199
    "idcliente" => 5459
    "factura" => 40635
    "fecha" => "2022-03-07"
    "baseimponible" => "148.00"
    "iva" => "31.08"
    "idiva" => 5
    "total" => "179.08"
    "vencimientos" => "2022-03-09"
    "domiciliaciones" => null
    "ccc" => null
    "observaciones" => null
    "autonomo" => null
    "razonsocial" => null
    "conceptos" => "POR SERVICIOS DE DESINSECTACION SEGÚN CONTRATO"
    "importes" => null
    "codigocliente" => null
    "formapago" => null
    "estado" => null
    "plazo" => null
    "tipopagos_id" => null
    "cobrado" => null
    "emailed" => null
    "remesada" => null
    "contrato" => array:76 [▶]
  ]
  5 => array:25 [▼
    "id" => 81184
    "idcliente" => 12421
    "factura" => 40620
    "fecha" => "2022-02-21"
    "baseimponible" => "54.00"
    "iva" => "11.34"
    "idiva" => 5
    "total" => "65.34"
    "vencimientos" => "2022-03-05"
    "domiciliaciones" => null
    "ccc" => null
    "observaciones" => null
    "autonomo" => null
    "razonsocial" => null
    "conceptos" => "POR SERVICIOS DE DESINSECTACION SEGÚN CONTRATO"
    "importes" => null
    "codigocliente" => null
    "formapago" => null
    "estado" => null
    "plazo" => "3"
    "tipopagos_id" => null
    "cobrado" => null
    "emailed" => null
    "remesada" => null
    "contrato" => array:76 [▶]
  ]
  7 => array:25 [▼
    "id" => 81191
    "idcliente" => 15869
    "factura" => 40627
    "fecha" => "2022-02-23"
    "baseimponible" => "89.86"
    "iva" => "23.89"
    "idiva" => 5
    "total" => "113.75"
    "vencimientos" => "2022-03-04"
    "domiciliaciones" => null
    "ccc" => null
    "observaciones" => null
    "autonomo" => null
    "razonsocial" => null
    "conceptos" => "POR NUESTRO SERVICIO DE CONTROL DE PLAGAS"
    "importes" => null
    "codigocliente" => null
    "formapago" => null
    "estado" => null
    "plazo" => "4"
    "tipopagos_id" => null
    "cobrado" => null
    "emailed" => null
    "remesada" => 1
    "contrato" => array:76 [▶]
  ]
  8 => array:25 [▼
    "id" => 81198
    "idcliente" => 5459
    "factura" => 40634
    "fecha" => "2022-03-04"
    "baseimponible" => null
    "iva" => "0.00"
    "idiva" => 5
    "total" => "0.00"
    "vencimientos" => "2022-02-17"
    "domiciliaciones" => null
    "ccc" => null
    "observaciones" => null
    "autonomo" => null
    "razonsocial" => null
    "conceptos" => "POR SERVICIOS DE DESINSECTACION SEGÚN CONTRATO"
    "importes" => null
    "codigocliente" => null
    "formapago" => null
    "estado" => null
    "plazo" => null
    "tipopagos_id" => null
    "cobrado" => null
    "emailed" => null
    "remesada" => null
    "contrato" => array:76 [▶]
  ]
  2 => array:25 [▼
    "id" => 63464
    "idcliente" => 4132
    "factura" => 27172
    "fecha" => "2016-02-22"
    "baseimponible" => "-282.65"
    "iva" => "-59.36"
    "idiva" => 5
    "total" => "-342.01"
    "vencimientos" => "2022-02-16"
    "domiciliaciones" => ""
    "ccc" => ""
    "observaciones" => ""
    "autonomo" => 1
    "razonsocial" => 1
    "conceptos" => ""
    "importes" => ""
    "codigocliente" => null
    "formapago" => "REBUT DOMICILIAT 15-8-15 CTA ES46.0075.0081.6106.01313202"
    "estado" => ""
    "plazo" => null
    "tipopagos_id" => null
    "cobrado" => null
    "emailed" => null
    "remesada" => null
    "contrato" => array:76 [▶]
  ]
  0 => array:11 [▼
    "id" => 254
    "idcliente" => 8383
    "vencimientos" => "2022-02-09"
    "conceptos" => "POR SERVICIOS DE DESINSECTACION SEGÚN CONTRATO"
    "plazo" => null
    "total" => "102.85"
    "observaciones" => null
    "idiva" => 5
    "iva" => "17.85"
    "baseimponible" => "85.00"
    "contrato" => array:76 [▶]
  ]

CodePudding user response:

When you sort collection by keys it will have structure like this:

[
  "0": [COLLECTION],
  "1": [COLLECTION],
  "2": [COLLECTION],
  "3": [COLLECTION],
  ...
]

So, when you return this it will be sorted by keys. You have to get values of that collections.

So, in your case you have to add ->values() method:

$data = collect($data)->sortByDesc('vencimientos')->values();       

Or if it doesn't work try this:

$data = collect($data)->sort(function ($a, $b) {
  return strtotime($a->vencimientos) < strtotime($b->vencimientos);
})
->values();

CodePudding user response:

You can use the method of PHP multisort for DESC or ASC sorting, it is for sorting multiple or multi-dimensional arrays:

  $array_multisort(array_map('strtotime',array_column($array,'vencimientos')),SORT_DESC,$array);
  • Related