1
So, I have 3 tables (doctors, addresses and phones), first I do the insertion in the medical table using the store method that is in its own controller, and I take the id created for it and insert in the other tables along with the other information. For this I called the other classes into this method, rather than using their methods.
The following code is working, but I’d like to know if this is really the best way to do it. Is it correct to do everything only in the medical class controller and ignore the insertion methods of the other classes? I couldn’t think of anything using the three-class controller.
I will not use any method of the telephone and address classes, because even in the edition they will be changed in the medical class. No problem deleting their controller and leaving only the Model right?
PS: I changed the code, now it’s smaller and cleaner.
Tables: medicos(id, nome, Descricao, id_cidade); enderecos(id, rua, numero, sala, id_medico); telefones(id, id_medico, numero). A doctor will only have one address (street, number, etc.) but may have more than one phone.
Medicocontroller
public function store(Request $request)
{
$dataForm = $request->all();
$medico = $this->medico->create($dataForm);
$dataForm['id_medico'] = $medico->id;
Endereco::create($dataForm);
foreach($dataForm['fone'] as $fone)
{
$dataForm['fone'] = $fone;
Telefone::create($dataForm);
}
}
Model Medico
namespace App\Models\painel;
use Illuminate\Database\Eloquent\Model;
class Medico extends Model
{
protected $fillable = [
'nome', 'id_cidade', 'created_at', 'updated_at'
];
}
Model Endereco
namespace App\Models\painel;
use Illuminate\Database\Eloquent\Model;
class Endereco extends Model
{
public $timestamps = false;
protected $fillable = [
'rua', 'numero', 'sala', 'id_medico'
];
}
Model Telefone
namespace App\Models\painel;
use Illuminate\Database\Eloquent\Model;
class Telefone extends Model
{
public $timestamps = false;
protected $fillable = [
'fone', 'id_medico'
];
}
There is, if code can be improved, but put the 3 models and their relationships I’m not understanding them
– novic
I changed the post, now maybe it’s easier to understand. Basically my question is q how the data will be in the same form, they will be sent to the same place (doctor’s controller), then the insertion of the three tables will be made in the same method. Leaving aside the methods of the other classes, is that correct? If yes, I will no longer use their methods, I can delete your controllers?
– Diego Vieira