Displaying filtered user data using Ajax and PHP
The fact that you  are making three separate trips to the server stands out as an obvious refinement opportunity.
We don't know exactly where your performance bottleneck is, but you can be sure that best practice is to reduce your total trips to ...well, anywhere.
Think about it like this:

Your mother tells you to go to the shop and get a dozen eggs.
You get on your bicycle and make 12 trips to the shop and deliver one egg to your mother per trip.
Your sister shakes her head disapprovingly.
Your mother tells the neighbors that she loves you because you are &quot;special&quot;.

If you know that the full flow of this task is to write data to the db ...then get some data from one table ...then get some data from another table, then the sensible thing is to dictate this flow entirely on the server side then only return to the client-side when everything is finished.
Effectively, I recommend using a single ajax request (with POST instead of GET because you are writing data to your database).
The dedicated server-side receiving script should execute the database writing query, then immediately execute the database fetching queries, then generate a single json string which contains all of the necessary user and usergroup data to satisfy your client-side processes.
If the bottleneck is isolated to the database interactions, then we don't have enough information to assist with optimizing the schema.

General advice:

Don't use mysqli_query() when you have a project that provides querying helper methods.  You whole application should use consistent techniques.

Only use concatenation when necessary.  '/users' + '/id' should be '/users/id'

When sending data to the server to fetch/read data from the db, use $_GET.  When sending to write/insert/update/delete data, use $_POST. There are exceptions, but this is the rule.

Use consistent spacing and tabbing in your code so that it is easier to maintain.

Don't use old school comma-join syntax -- use JOINs.

This is weird:
$i = 0;
if ($usergroups-&gt;num_rows != 0){
  foreach ($usergroups as $usergroup) {
    $ids[$i] = $usergroup;
    $i++;
  }
} else {
  $ids[$i] = 0-1;

}

Why declare a counter?  Why subtract 1 from 0?  You should probably trash most this section of code, and maybe entertain something closer to...
foreach ($usergroups as $usergroup) {
    $ids[] = $usergroup;
}

$data = \DB::table('grupo')
    -&gt;whereNotIn('grupo.idgrupo', $ids ?? -1)
    -&gt;get();


Never omit curly braces in an attempt to reduce the size of your code.  If you have a simple condition and like ternary expressions, use those instead.

Try to avoid declaring single-use variables.

If type dictates location, then don't send both parameters to the next function scope.  Generate location conditionally when you get there.