Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ifrit's avatar
Level 2

Getting a list of new and old values

I have a table that as a list of roles and what I'm trying to do is when a user uploads a new position it needs to compare the differences between the new roles and the old roles.

So what I'm trying to do is get some kind of list that will give me a list of missing roles and the price that they have in the roles tabled pased on the positions ID.

For example

My roles table looks like this

    id | name          | salary | position_id
    1  | Admin         | 1234   |     1
    2  | Manager       | 1111   |     1
    3  | Sales Manager | 1111   |     1
    4  | Clerk         | 10     |     2

So when the user uploads a new role for a specific position my roles table will look like this

    id | name          | salary | position_id
    1  | Admin         | 1234   |     1
    2  | Manager       | 1111   |     1
    3  | Clerk         | 10     |     2
    4  | Admin         | 2222   |     3
    5  | Manager       | 3333   |     3
    6  | Sales Manager | 1111   |     3

And what I'm trying to do is (in this example) is grab Admin and Manager where the position is the old position EG: position_id = 1 and where the position is the new position EG: position_id = 3 and where the price is different

Here is my code

    public function changeRoles(Position $position)
    {
        $old_roles = Role::where('position_id', $position->id)->get();

        $new_position = Position::find(request('position'));
        $new_roles = Role::where('position_id', $new_position->id)->get();
        
        $old_arr = [];
        $old_salary = [];

        foreach($old_roles as $old_role)
        {
            $old_arr[] = $old_role->name;
            $old_salary[] = $old_role->salary;
        }

        $new_arr = [];
        $new_salary = [];

        foreach($new_roles as $new_role)
        {
            $new_arr[] = $new_role->name;
            $new_salary[] = $new_role->salary;
        }

        // grabing only the totals where there is a difference between the old one and new one
        $total_diff = array_diff($new_salary, $old_salary);
        
        
        // List all new and old stuff
	$newStuff = Role::whereIn('position_id', [$new_position ->id, $position_id->id])
                                ->whereColumn('salary', '!=' ,'salary')
                                ->get();

    }
0 likes
0 replies

Please or to participate in this conversation.