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.
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 ??