Q:

Laravel - How to specify the User Role to be displayed

I am working on a Web Application using Laravel-5.8 as the backend.  I am combining two classes: User and Role
ApiController.php
public function store(Request $request)
{
    if(!Auth::user()->hasPermissionTo('Add Users'))
        return response()->json([ "message" => 'User do not have permission'], 401);

    $request->validate([
        'name' => 'required|string|min:2',
        'email' => 'required|string|email|unique:users',
        'password' => 'required|string|confirmed|min:6'
    ]);

    $user = new User([
        'name' => $request->name,
        'email' => $request->email,
        'password' => bcrypt($request->password),
        'activation_token' => str_random(60),
        'address' => $request->email,
        'about' => $request->email,
    ]);

    if($request->role)
        foreach($request->role as $role)
            $user->assignRole($role);
    else {
        return response()->json([
            'error' => 'Role Not Found!' 
        ], 401);
    }

    $user->school_id = Auth::user()->school_id;

    if($request->address)
        $user->address = $request->address;

    if($request->about)
        $user->about = $request->about;

    $user->save();

    $avatar = Avatar::create(strtoupper($user->name))->getImageObject()->encode('png');
    Storage::put('avatars/'.$user->id. '/avatar.png', (string) $avatar);
    $user->notify(new SignupActivate($user));

    return response()->json([
        'message' => 'Successfully Added New User!' 
    ], 201);
}

From the code above, I don't want to display all the Roles.  I want to use the foreach statement to only select and display these two Roles: Admin, Staff. 
That is, where role_name = Admin, Staff. 
foreach($request->role as $role)
    $user->assignRole($role);

How do I achieve this? 

A:

I think you can go about that in this way. 

You can use the Laravel filter method which is in the documentation here Laravel Collection filter Method
This method returns an array of items that have passed a given condition
You convert your roles request into a collection first
Then use a condition like 
return if($role == 'Admin' || $role == 'Staff')
Where you see return $value > 2;

$collection = collect([1, 2, 3, 4]);

$filtered = $collection->filter(function ($value, $key) {
    return $value > 2;
});

$filtered->all();
// [3, 4]

