Q:

hide confidential data for multiple users

I have an user named eric which has like user role Former.

I would like the user Eric sees only his confidential information but not those of others.

Would you have any idea how I could make this?
Thank you for your help.
Edit Controller Former
public function index(Request $req)
{   

if ($req->search == "") {
    $formers = Former::paginate(5);

    $formerIdsDown = Cours::where('date_seance', "<=" , Carbon::now())->pluck('fk_former')->toArray();
    return view('admin.formers.index', compact('formers', 'formerIdsDown'));

} else {

    $validated = $req->validate([
        'search' => 'alpha', 
    ]);

    $formers = Former::where('nom', 'LIKE', '%' . $validated['search'] . '%')->paginate(5);
    $formers->appends($req->only('search'));

    $bikeIdsDown = Cours::where('date_seance', "<=" , Carbon::now())->pluck('fk_former')->toArray();

    return view('admin.formers.index', compact('formers', 'formerIdsDown'));
     }

$user = User::when($has_role, function ($query) {
    return $query->where('id', auth()->id()); 
    })
    ->get();

}

A:

You could handle this in two ways.
Limiting the data from the server
This should be your approach. Here you could add a condition in your query to know if the user has the restricted role in order to limit the data:
# InSomeController.php

$has_role = auth()->user()->hasRole('former'); // checking if the user has the role

$user = User
    ::when($has_role, function ($query) {
        return $query->where('a_column', 'a_value'); // The limitation you want to apply
    })
    ->get(); // executing the query

Limiting in the view
In case you are using blade templating engine.. you can make use of some Blade directives:
# in_some_view.blade.php

@if (auth()->user()->hasRole('former'))
    /** Your limited data goes here.. */ 
@else
    /** The default data goes here.. */
@endif

Obs: In both cases I used the hasRole('role') method, replace this with the logic to validate if the user has a given role.

