Can I use the same driver to register two types of people?

2

Well I have a form in php I have a select in which I choose if the type of client to register is natural or legal, by selecting which of the two shows the corresponding form. The problem is that to register each client type I am using two controllers, which I'm not sure is the right thing to do.

Database.

Natural Client Driver

public function store(ClientesFormRequest $request)
{
    if ($request) {
      try {
        DB::beginTransaction();
          $persona_natural = new Persona_natural;
          $persona_natural->telefono = $request->get('telefono');
          $persona_natural->cedula = $request->get('cedula');
          $persona_natural->direccion = $request->get('direccion');
          $persona_natural->nombre = $request->get('nombre');
          $persona_natural->save();

          $cliente = new Cliente;
          $mytime = Carbon::now('America/Bogota');
          $cliente->fecha_inscripcion = $mytime->toDateTimeString();
          $cliente->persona_natural_idpersona_natural = $persona_natural->idpersona_natural;
          $cliente->estado = 'Activo';
          $cliente->save();
        DB::commit();
      } catch (Exception $e) {
        db::rollback();
      }

      return redirect::to('cliente/natural');
    }
}

Driver Legal Client

public function store(JuridicosFormRequest $request)
    {
      if ($request) {
        try {
          DB::beginTransaction();
            $juridico = new Persona_juridico;
            $juridico->rason_social = $request->get('rason_social');
            $juridico->ruc = $request->get('ruc');
            $juridico->tipo_entidad = $request->get('tipo_entidad');
            $juridico->correo_electronico = $request->get('correo_electronico');
            $juridico->persona_natural_idpersona_natural = $request->get('persona_natural_idpersona_natural');
            $juridico->save();

            $cliente = new Cliente;
            $mytime = Carbon::now('America/Bogota');
            $cliente->fecha_inscripcion = $mytime->toDateTimeString();
            $cliente->juridico_idjuridico = $juridico->idjuridico;
            $cliente->estado = 'Activo';
            $cliente->save();


          DB::commit();
        } catch (Exception $e) {
          db::rollback();
        }

        return redirect::to('cliente/natural');
      }
    }

Requests. Natural client

public function rules()
    {
        return [
            'telefono'=>'max:20',
            'cedula'=>'required|max:10',
            'direccion'=>'required|max:200',
            'nombre'=>'required|max:50'
        ];
    }

Legal client

public function rules()
    {
        return [
            'rason_social',
            'ruc',
            'tipo_entidad',
            'correo_electronico',
            'persona_natural_idpersona_natural'
        ];
    }

To my way of thinking I think the correct thing would be to place an if that allows me to validate the type of client through the foreingKey that I will register in the client table, that is, if what my client receives. driver is a legal_idjuridico , then I enter the information in the legal table, and if it is what it receives is persona_natural_idpersona_natural then the insertion should be made in the persona_natural table, but I have no idea how to do it. Should I validate that with php ??

    
asked by jeancarlos733 08.12.2016 в 02:32
source

2 answers

3

I dare to say that this question gets answers that are based on opinions, even so I will answer it to guide a little and improve the practices of the person who asks:

  • If you have at least two types of clients, I assume that you have a trait ClienteController that does the common tasks between both types of clients and of this both clients inherit, besides having an interface implemented to respect the methods who must have both clients.

  • It should not be the responsibility of the controller to have to deal with the database and models, its function is simply to delegate this function to a service or a repository.

  • Using new Algo goes against the good practices of Laravel and "modern" programming. This generates unnecessary dependencies and makes it more difficult to maintain and test the code, for that there is dependency injection, just as you do with the Request.

  • I would use a single Request to validate both forms, and I would use a single method store() , and from there I would call the corresponding repository to store the data.

        
    answered by 08.12.2016 / 02:47
    source
    1

    The best way is to unify in a controller. You pass a parameter that indicates the type of client. Then when wanting to insert in a table or another, use the type of client you had received. Example:

    $tipo = $request->get('rason_social');
    if($tipo == 'natural'){
        // Insertar en tabla persona_natural.
    }else {
         // Insertar en tabla juridico
    }
    

    The above code is just an idea to be understood.

    Regarding validation, you should always do so as not to insert any value in your table. And that you should control. I do it on both sides (client (web browser) and server), but it depends on you. But it is advisable and necessary that you validate at least on the server side. In your case in the php.

        
    answered by 08.12.2016 в 02:41