I'm working with a database already populated. It has four tables (Student, Clinical, Family, and Compulsory Type), where Student receives foreignkey from the three tables;
Model Student
class Aluno extends Model{
public $timestamps = false;
protected $fillable =[
'Nome','Sobrenome','Cpf','Email','Altura','Telefone','Endereco'
];
public function clinico(){
return $this->belongsTo(Clinico::class);
}
public function familia()
{
return $this->belongsTo(Familia::class);
}
public function tipocompulsao()
{
return $this->belongsTo(Tipocompulsao::class, );
Clinical model
class Clinico extends Model
{
public $timestamps = false;
protected $fillable=[
'hasAnsiedade',
'hasInsonia',
'hasHipertensao',
'hasDiabetes',
'hasAlergias',
'hasDisturbiosOncologicos',
'hasProblemasRenais',
'hasMenopausa',
'hasHipotiroidismo',
'hasColesterol',
'hasFigado',
'hasOutros'
];
public function aluno() {
return $this->hasOne(Aluno::class );
}
}
model family
class Familia extends Model
{
public $timestamps = false;
protected $fillable=[
'hasObesidade',
'hasDoencaRenal',
'hasHipertensao',
'hasColesterolGorduraFigado'
];
public function aluno() {
return $this->hasone(Aluno::class);
}
}
Tipocompulsao
class Tipocompulsao extends Model{
public $timestamps = false;
protected $fillable=[
'hasCompulsaoDoce',
'hasCompulsaoSalgado'
];
public function aluno() {
return $this->hasone(Aluno::class);
}
}
Controller to save dataform
public function update(Request $request,$id)
{
$tipoco=Tipocompulsao::update($dataform);
$clinico= Clinico::update($dataform);
$familia=Familia::update($dataform);
$aluno=Aluno::update ($dataformU)->where($id);
$aluno->familia()->associate($familia);
$aluno->clinico()->associate($clinico);
$aluno->tipocompulsao()->associate($tipoco);
$aluno->save();
The main problem I wanted to solve is: how to do UPDATE on tables with 1 to 1 relationship?