2
I am working with a database already populated. It has four tables (Aluno
, clinico
, Familia
and TipoCompulsao
), where Aluno
receives foreignkey of the three tables;
Student model
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);
}
}
Typocompulsion
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 test(){
$dataform=([
'Nome'=> 'bi',
'DataNascimento'=>'21-01-1999',
'Sobrenome'=>'carlos',
'hasHipertensao'=>'1',
'hasObesidade'=>'1',
'hasDoencaRenal'=>'1'
]);
$tipoco=Tipocompulsao::create($dataform);
$clinico= Clinico::create($dataform);
$familia=Familia::create($dataform);
$aluno=$familia->aluno()->create($dataform);
$aluno=$clinico->aluno()->create($dataform);
$aluno=$tipoco->aluno()->create($dataform);
I’m using this script to save; the main problem is that it generates me three results in the table Aluno
one id
of each of the other tables for each row, while only one result.
But I found this. https://appdividend.com/2017/10/12/laravel-one-to-one-eloquent-relationships/ might help.
– Jasar Orion
Possible duplicate of Save Relationship 1:1 on Laravel 5.3
– novic
You could have put at least the relationship keys to have a minimal example, but there is already a question about this type of relationship: https://answall.com/questions/175842/salvar-relationship-11-no-laravel-5-3/175886#175886
– novic
Another example link: https://answall.com/questions/152089/problemas-com-related-um-para-muitos-laravel/152108#152108
– novic