How to do Insert 1 to 1, with four tables in Laravel

Asked

Viewed 149 times

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.

  • 1

    Possible duplicate of Save Relationship 1:1 on Laravel 5.3

  • 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

  • Another example link: https://answall.com/questions/152089/problemas-com-related-um-para-muitos-laravel/152108#152108

1 answer

2


Friend, you have to do a review. In your code there are some typos.. e.g. There are models that have hasOne all tiny.

To relate 1 to 1 is used the 'Associate':

$clinico = Clinico::create($dataform);
$familia = Familia::create($dataform);


$clinico->familia()->associate($familia);
$clinico->save();

To disassociate:

$clinico->familia()->dissociate();
$clinico->save();

This arrow foreign key to null.

  • Friend sorry for the clerical error! I was trying to find a solution on everything and forum and you saved my week literally.Thank you so much !!!

  • Blz bro. I’m glad you solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.