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, when selecting either of the two the corresponding form is displayed. The problem is that to register each client type I am using two controllers, which I am not sure is correct.
Natural Client Controller
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');
}
}
Legal Client Controller
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 customer
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'
];
}
In my opinion, I think that the correct thing would be to place an if that allows me to validate the type of client through the foreignKey that I am going to register in the client table , that is, if what my controller receives is a legal_idlegal , then I enter the information in the legal table , and if what you receive is natural_person_id_natural_person then the insertion should be done in the natural_person table, but I have no idea how to do it. should i validate that with php??
I dare to say that this question gets answers that are based on opinions, still I am going to answer it to guide a little and improve the practices of the person asking:
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 from this both clients inherit, in addition to having an interface implemented to respect the methods that both clients must have.It should not be the controller's responsibility to deal with the database and models, its function is simply to delegate this function to a service or a repository.
Using
new Algo
is against Laravel best practices 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 like you do with 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.The best way is to unify in a controller. You pass it a parameter that indicates the type of client. Then when wanting to insert in one table or another, use the type of client that you had received. Example:
The code above is just an idea for you to understand.
As for validation, you should always do so as not to insert any value in your table. And you should control that. I do it on both sides (client (web browser) and server), but it's up to you. But it is recommended and necessary that you validate at least on the server side. In your case in the php.