dimanche 26 mai 2019

Recursive menu in laravel

I am building a recursive menu in the following way:

public static function tree()
{
    return static::with(implode('.', array_fill(0, 100, 'children')))->where('parent_id', '=', '0')
        ->join('company_components', 'id', '=', 'company_components.component_id')
        ->where('company_components.company_id', '=', auth('api')->user()->with('company')->first()->company_id)
        ->orderBy('id')
        ->get();
}

I get something like this:

[
  {
    "id":1,
    "parent_id":0,
    "name":"Cuentas",
    "url":"",
    "icon":"fa fa-user",
    "deleted_at":null,
    "created_at":"2019-05-26 18:57:02",
    "updated_at":"2019-05-26 18:57:02",
    "company_id":1,
    "component_id":1,
    "children":[
        {
          "id":2,
          "parent_id":1,
          "name":"Crear Cuenta",
          "url":"/account/create",
          "icon":"fa fa-circle-o",
          "deleted_at":null,
          "created_at":null,
          "updated_at":null,
          "children":[]
        }
     ]
  }
]

How can I avoid the empty array of the last elements (which no longer have children)?

Is there any way that the element is not created if "children" if it does not have more children?



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/30Ld0Oz
via IFTTT

Aucun commentaire:

Enregistrer un commentaire