Register N for 1 and N for N?

Asked

Viewed 139 times

0

How can I register in a relationship 1-N and of N para M, I’m doing it this way, but it’s not cool:

$endereco = new endereco;
$endereco->complemento = $request->complemento;
$endereco->logradouro = $request->logradouro;
$endereco->bairro = $request->bairro;
$endereco->cidade = $request->cidade;
$endereco->estado = $request->estado;
$endereco->cep = $request->cep;
$endereco->save();   

$civil = new civil;
$civil->nome = $request->nome;
$civil->cpf = $request->cpf;
$civil->matricula = $request->matricula;
$civil->data_nascimento = $request->data_nascimento;
$civil->pai = $request->pai;
$civil->mae = $request->mae;
$civil->situacao = $request->situacao;
$civil->sexo = $request->sexo;
$civil->matricula = $request->matricula;
$civil->estado_civil = $request->estado_civil;
$endereco->civil()->save($civil);    
  • What would be cool?

  • https://answall.com/questions/236486/insert-many-to-many-laravel-5-4/236600#236600 an example

  • I’ve noticed that you have some questions, but you don’t interact with the community, because?

  • Sorry. It’s just that I don’t know how to do it. If you follow this pattern the code will get too big. not to mention that I can not make the relationship of many to many in Aravel. If you can help me I will be very grateful.

  • you need to make both classes available! so that I can see, it doesn’t seem difficult to solve, but, I don’t know what you did in the model classes! and you have open questions, it is good to interact and credit if solve your problems, help and many other people

1 answer

0

If I’m not mistaken, the address key is on the civilian side. The sequence of the entities is corecta, but I see a problem on the line: $endereco->civil()->save($civil);. If you want to "set" the address code you should see something like $civil->endereco_id = $endereco->id; and finally $civil->save();

I advise you to see about Eloquent Relationships in https://laravel.com/docs/5.5/eloquent-relationships

Browser other questions tagged

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