Home > Net >  php arrange repeating items in an object
php arrange repeating items in an object

Time:09-21

this object:

"permission_category": [{
        "id": 2,
        "name": "user",
        "permissions": {
            "id": 2,
            "name": "userCreate"
        }
    },
    {
        "id": 2,
        "name": "user",
        "permissions": {
            "id": 3,
            "name": "userUpdate"
        }
    }
]

can i convert it to this?

"permission_category": [{
        "id": 2,
        "name": "user",
        "permissions": [
            {
              "id": 2,
              "name": "userCreate"
            },
            {
              "id": 3,
              "name": "userUpdate"
            },
        ]
    }
]

Do I have such a chance? Because if I return this with foreach it will print the same category name more than once. Is it possible to show both userCreate and userUpdate permissions belonging to the user category under one category? The object looks like this to me. I have created a relationship between 4 tables in Laravel. I will share their code with you too. Please tell me if what I need to do is change the code between relationships. Please tell me if what I need to do is to edit the array in the output with php array functions. I don't know anything about this, I need some help. Sorry for prolonging the topic. I'm sure there's a right and easy way to do this. Since I don't know, I had to ask. I would like to express my thanks and regards to those who have helped in advance.

Laravel Relationship:

UsersController.php

public function add() {
        $data = array();
        $user = AdminUsers::where('id', $this->uid())->first();
        if($user->hasAdministrator()->first())
            $data['permission_category'] = PermissionCategory::with('permissions')->get();
        else
            $data['permission_category'] = PermissionsResource::collection($user->permissions()->with('category')->get());
        return $data;
        die();
        return view('admin.users.add', $data);
    }

PermissionsResource.php

return [
            "id" => $this->category[0]->id,
            "name" => $this->category[0]->name,
            "permissions" => [
                "id" => $this->id,
                "name" => $this->name
            ],
        ];

AdminUsers.php (Model)

    public function permissions() {
        return $this->belongsToMany(Permissions::class, 'admin_perms');
    }

Permissions.php (Model)

    public function category() {
        return $this->belongsToMany(PermissionCategory::class, 'permissions', 'id');
    }

admin_users table:

-id
-username

permission_category table:

-id
-name

permissions table:

-id
-permission_category_id
-name

admin_perms table:

-id
-admin_users_id foreign key
-permissions_id foreign key

CodePudding user response:

in permission category model:

public function permissions() {
    return $this->hasMany(Permissions::class, 'permission_category_id', 'id');
}

in permissions model

public function adminPerms() {
    return $this->hasMany(AdminPerms::class, 'permissions_id', 'id');
}

then

$permissionCategoriesWithUserPermissions = PermissionCategory::with('permissions')
    ->whereHas('permissions', function ($q) use ($user){
        $q->whereHas('adminPerms', function ($query) use ($user){
            $query->where('admin_users_id', $user->id);
        });
    })->get();

PermissionCategoryResource::collection($permissionCategoriesWithUserPermissions);

and in PermissionCategoryResource:

return [
    "category_id" => $this->id,
    "category_name" => $this->name,
    "permissions" => PermissionsResource::collection($this->permissions),
];

and in PermissionsResource:

return [
    "permission_id" => $this->id,
    "permission_name" => $this->name,
];

this structure should work out.

  • Related