erickun
2 weeks ago

Improve Code

Posted 2 weeks ago by erickun

Hi guys, i would like to clean this piece of code, if you can help me please.

In my RoleController on create method i have this...

public function create()
    {
        $role = new Role;
        $abilities = Ability::all();
        return $abilities->groupBy(function($item, $key){ return substr(strrchr($item['name'], '-'), 1); });
        return view('backend.roles.create', compact('role','abilities'));
    }

and the results

{
"users": [
{
"id": 1,
"name": "browse-users",
"label": "Browse Users",
"created_at": "2020-03-22T01:53:07.000000Z",
"updated_at": "2020-03-22T01:53:07.000000Z"
},
{
"id": 2,
"name": "read-users",
"label": "Read Users",
"created_at": "2020-03-22T01:53:07.000000Z",
"updated_at": "2020-03-22T01:53:07.000000Z"
},
{
"id": 3,
"name": "edit-users",
"label": "Edit Users",
"created_at": "2020-03-22T01:53:07.000000Z",
"updated_at": "2020-03-22T01:53:07.000000Z"
},
{
"id": 4,
"name": "add-users",
"label": "Add Users",
"created_at": "2020-03-22T01:53:07.000000Z",
"updated_at": "2020-03-22T01:53:07.000000Z"
},
{
"id": 5,
"name": "destroy-users",
"label": "Destroy Users",
"created_at": "2020-03-22T01:53:07.000000Z",
"updated_at": "2020-03-22T01:53:07.000000Z"
}
],
"roles": [
{
"id": 6,
"name": "browse-roles",
"label": "Browse Roles",
"created_at": "2020-03-22T01:53:07.000000Z",
"updated_at": "2020-03-22T01:53:07.000000Z"
},
{
"id": 7,
"name": "read-roles",
"label": "Read Roles",
"created_at": "2020-03-22T01:53:07.000000Z",
"updated_at": "2020-03-22T01:53:07.000000Z"
},
{
"id": 8,
"name": "edit-roles",
"label": "Edit Roles",
"created_at": "2020-03-22T01:53:08.000000Z",
"updated_at": "2020-03-22T01:53:08.000000Z"
},
{
"id": 9,
"name": "add-roles",
"label": "Add Roles",
"created_at": "2020-03-22T01:53:08.000000Z",
"updated_at": "2020-03-22T01:53:08.000000Z"
},
{
"id": 10,
"name": "destroy-roles",
"label": "Destroy Roles",
"created_at": "2020-03-22T01:53:08.000000Z",
"updated_at": "2020-03-22T01:53:08.000000Z"
}
],
"abilities": [
{
"id": 11,
"name": "browse-abilities",
"label": "Browse Abilities",
"created_at": "2020-03-22T01:53:08.000000Z",
"updated_at": "2020-03-22T01:53:08.000000Z"
},
{
"id": 12,
"name": "read-abilities",
"label": "Read Abilities",
"created_at": "2020-03-22T01:53:08.000000Z",
"updated_at": "2020-03-22T01:53:08.000000Z"
},
{
"id": 13,
"name": "edit-abilities",
"label": "Edit Abilities",
"created_at": "2020-03-22T01:53:08.000000Z",
"updated_at": "2020-03-22T01:53:08.000000Z"
},
{
"id": 14,
"name": "add-abilities",
"label": "Add Abilities",
"created_at": "2020-03-22T01:53:08.000000Z",
"updated_at": "2020-03-22T01:53:08.000000Z"
},
{
"id": 15,
"name": "destroy-abilities",
"label": "Destroy Abilities",
"created_at": "2020-03-22T01:53:08.000000Z",
"updated_at": "2020-03-22T01:53:08.000000Z"
}
]
}

Thank you

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