samedi 8 décembre 2018

Laravel collection grouped by property array

My boss has asked me for a report, and I don't want to do many loops to obtain the information if I can do it in a single Eloquent sentence. I have this model:

{
   "id":1,
   "persona_id":75,
   "localidad_id":1,
   "datos":{
      "web":null,
      "lider":{
         "persona":"18"
      },
      "redes":{
         "email":null
      },
      "activa":"true",
      "nombre":"Comisión Ambiental Local - Usaquén",
      "asistente":"true",
      "secretaria":"true"
   },
   "created_at":"2018-06-15 20:01:40",
   "updated_at":"2018-11-02 20:37:02"
}

The model is called Instancia and it has a Json field called datos which is casted to an array every time the model is created using the protected attribute $casts.

Inside that the datos array, I have a key called lider and inside that one, another called persona. I want if possible to group my collection by that one parameter persona.

I have tried several approaches, but non has worked:

$instancias = Instancia::where( 'datos->lider->persona', '<>', null )->get()->groupBy( 'datos->lider' );

That one returns the entire collection now grouped by a "" parent.

$instancias = Instancia::where( 'datos->lider->persona', '<>', null )->get()groupBy( 'persona' );

Any help will be appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire