How can you allow each user to edit their profile, use Laravel, and a generated one called InfyOm?

0

Good I am new in this, I am doing a small system with the framework of PHP Laravel, to start create a CRUD of a table that I have in a database of MySQL with the generator of InfyOm (Here you can see it and download it < a href="http://labs.infyom.com"> link ), anyone can register and operate said CRUD

What I want to do now is that in a tab that says "Profile" users can edit their information that they registered when they registered, if I make a CRUD of the users table, all users will be able to edit the information of any user, Can someone help me, please.

    
asked by Jose L. 03.08.2018 в 03:55
source

1 answer

0

What you can do is create a route that loads a function similar to edit () but that does not receive any parameter, but you get the user's id by means of the session variable Auth :: User () - > id , this loads the logged user's data and sends it to a form so that it can be edited by the user. Then to send the data you do something similar to the previous route where you create a route to a function update () that does not receive parameters and you get the user id of the same as mentioned before and you simply update the user data.

Example

routes

Route::get('perfil/actualizar',['as'=> 'perfil.edit', 'uses' => 'UsuarioController@edit']);
Route::patch('perfil/actualizar',['as'=> 'perfil.update', 'uses' => 'UsuarioController@update']);

driver using InfyOm

public function edit(){
   $usuario = $this->userRepository->findWithoutFail(Auth::User()->id);
   if(empty($usuario)){
      Flash::error('mensaje error');
      return redirect()->back();
   }
   return view('editar_perfil')->with('usuario', $usuario);
}

public function update(UpdateUserRequest $request){
   $usuario = $this->userRepository->findWithoutFail(Auth::User()->id);
   if(empty($usuario)){
      Flash::error('mensaje error');
      return redirect()->back();
   }
   $input=$request->all();
   $usuario = $this->userRepository->update($input,Auth::User()->id);
   Flash::success('Perfil actualizado con éxito.');
   return redirect(route('index'));
}

driver without using InfyOm

    public function edit(){
       $usuario = User::find(Auth::User()->id);
       if(empty($usuario)){
          Flash::error('mensaje error');
          return redirect()->back();
       }
       return view('editar_perfil')->with('usuario', $usuario);
    }

   public function update(Request $request){
       $usuario = User::find(Auth::User()->id);
       if(empty($usuario)){
          Flash::error('mensaje error');
          return redirect()->back();
       }
       $usuario->fill($request->all());
       $usuario->save();
       Flash::success('Perfil actualizado con éxito.');
       return redirect(route('index'));
    }

view

<form action="{{route('perfil.update')}}" method="PATCH">
 /** lo que vaya aqui **/
</form>

This way you avoid creating a whole crud to only update the prfil of the users, and if you want to add a view show () , you only do something similar to edit ()

However, if you want to use the InfyOm commands to stop a step, you can use this php artisan infyom:scaffold $MODEL_NAME --views=index,create,edit,show

Where you use - views = to specify the views you want to generate.

Here you can find more information: link

    
answered by 03.08.2018 в 04:58