Create profiles in application

Asked

Viewed 781 times

5

I need to modify my application so that each user sees its particular content, because each user will have information of its own. I’ve set up the default authentication using make auth and it’s working fine.

2 answers

2

Now you need to create a middleware to restrict access to the page; put the following code inside your controller’s constructor.

    $this->middleware(function($request, $next) {

       if(auth()->user()->id == $sua_variavel_do_usuario_dono_do_perfil){
              retrun redirect('/outra-pagina'); // se o usuario não for o mesmo do perfil ele é redirecionado para outra página.
             }

       return $next($request);
       }
  • Inside the user controller? I will need to Binding on the routes to reference each user’s id??

  • Okay, I’m gonna go over here and test, whatever I tell you, thank you.

  • Yes, or you can use your controller to pick up the logged in user, passing to a variable, with this in your method you search in the database the information you want of the given user and presents on the screen in a route type "/profile". With this only the user owner of the information can see, because if another user tries to access, he will search only your information!

  • Would you show me a more detailed example, without wanting to abuse, I’m beginner in Latin.

1


Here’s an example where I display user services.

class ServicoController extends Controller {

        private $usuario;

        public function __construct()
        {
            $this->middleware('auth');
            $this->middleware(function($request, $next){
                $this->usuario = auth()->user(); //Aqui você pega o usuario logado

                return $next($request);
            });

            public function index(){
                  $servico = Servico::where('user_id',$this->usuario->id)->get(); // Com isso você vai sempre buscar (como no meu caso) os serviços do usuário logado, ou seja, nessa rota cada usuário só vai conseguir ver seus serviços
            }

        }
  • Okay thanks for the help.

  • Helped? If yes, please mark as correct!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.