Write to two different tables using the same View

Asked

Viewed 2,912 times

2

I am registering suppliers, and I need to record it in a table and your address in another. I use a single view suppliers. to fill in all the data. How can I separate this data to save it to their respective tables?

Suppliers Table:

id          | int
nome        | varchar
endereco_id | int

Address Table:

id          | int
logradouro  | varchar
bairro      | varchar
cep         | int

Controller:

public function store(Request $request)
{
    $input = $request->all();
    Fornecedor::create($input);
    return redirect('fornecedores');
}

Model:

public function endereco() {
    return $this->hasMany('App\Endereco', 'id', 'endereco_id');
}
  • The problem is getting the id address and write to the supplier?

  • @rray, too. All data is populated in the vendor view. The action of this form goes to the vendor controller. I want to separate personal data and address, record each in their respective table and then make the relationship.

2 answers

3


Solved: Within the method store() from Vendorcontroller I separated the fields for each table instead of using $request->all().

Vendorcontroller:

public function store(Request $request)
{
    $endereco = new Endereco();
    $endereco->logradouro = $request->get('logradouro');
    $endereco->numero = $request->get('bairro');
    $endereco->numero = $request->get('cep');
    $endereco->save();
    $endereco_id= $endereco->id;

    $fornecedor = new Fornecedor();
    $fornecedor->cnpj = $request->get('cnpj');
    $fornecedor->razao_social = $request->get('razao_social');
    $fornecedor->endereco_id = $endereco_id;
    $fornecedor->save();

    return redirect('view');
}

I do not know if it is the best way (good practice) to do this, but solved the problem.

1

Talk buddy, all right? Come on!

Apparently the case is of hasOne relationship... as well?

Your suppliers table works with n:1 relationship for addresses.

Fixing that in your model you have defined as hasMany, that free translation is "contains several". Ie, fornecedores contém vários endereços. Which according to your schema is not quite so, in it the provider contains only ONE address, as it works with FK direct to addresses.

The eloquent of the alternatives to working with relationships, in a way practically papaya with sugar.

$fornecedor = new Fornecedor();
$fornecedor->cnpj = $request->get('cnpj');
$fornecedor->razao_social = $request->get('razao_social');
$fornecedor->endereco->logradouro = $request->get('logradouro');
$fornecedor->endereco->bairro = $request->get('bairro');
$fornecedor->endereco->cep = $request->get('cep');
$fornecedor->push();

In this example "push" will occur. When saving the provider it will save the address and already do the link for you.

You can check in the manual this method: http://laravel.com/docs/4.2/eloquent#relationships

Remembering that you need to make the relationship of: fornecedores belongsTo enderecos

The push it checks every relation and performs the commands, if you do not like the use of it, you can choose to associate.

Good, doubts of a touch!

  • Thank you, @juniorb2ss. I’ll test it tonight. I imagined that there was a formula "magic" to do this, but since I was in a bit of a hurry, I decided to do it manually anyway. I have other similar relationships and I’ll try to use it that way.

  • @Buback Aaah has yes, eloquent is full of magical methods, even more for relationship work. Only problem is that some things the querys are not as performative :(

  • I tried to do what I said in another example. Using push(). But it didn’t work. http://answall.com/questions/86714/related-nn-no-laravel-n%C3%A3o-grava-attributes-of-object

Browser other questions tagged

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