Saving data with hasMany relationship

Asked

Viewed 398 times

4

I’m trying to do an update and three tables related to Laravel, the past data comes from a single view, and the relationships have been made, should be the update action of my controller that is not right follows below.

Update of CorretoresControllers

public function update(Corretor $corretor, Request $request) {
    $corretor->update($request->all());
    return redirect('corretores');
}

Model Corretor

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Endereco;
use App\Contato;
use App\Documento;

class Corretor extends Model {

    protected $fillable = ['nome', 'email'];
    protected $table = 'corretors';

    public function endereco() {
        return $this->hasMany('App\Endereco');
    }

    public function contato() {
        return $this->hasMany('App\Contato');
    }

    public function documento(){
        return $this->hasMany('App\Documento');
    }
}

When the one dd($request->all()); returns the following data:

array:11 [▼
  "_method" => "PUT"
  "_token" => "sWlp1StEgwx7PXZbjo7nV8eYecKpic32E21YeWxA"
  "nome" => "Bruno Neves 1"
  "email" => "[email protected]"
  "logradouro" => "TV DO PARAISO 214"
  "numero" => "10"
  "telefone" => "9632242778"
  "celular1" => "9999-9999"
  "celular2" => "9999-9999"
  "cpf" => "123"
  "rg" => "123"
]

Already quado do um dd($corretor); returns only these data:

"id" => "13"
    "nome" => "Bruno Santos"
    "email" => "[email protected]"
    "created_at" => "2016-01-26 00:55:11"
    "updated_at" => "2016-02-04 03:14:27"
  • Ai Bruno the answer of the companion solved his problem?

1 answer

2


Whenever saving data on Laravel you need to define in the model the fields that are "fillable" in the case of $fillable.

Add the table fields you want to add in $fillable. I also always change the table and place a new field forget to change the value of this property.

Saving data in relationships

If you intend to save the relationship data, you need to specify this as this is not done automatically.

To save data from a relationship hasMany it is necessary to use the saveMany.

$documentos = [
   new App\Documento($dadosDocumento)
];

$enderecos = [
   new App\Endereco($dadosEndereco),
];

$contato->fill($dadosContato)->save();

$contato->documentos()->saveMany($documentos);

$contato->enderecos()->saveMany($enderecos);

I separated the data into different variables, like $dadosEndereco and $dadosContato. I usually use the method only. It can be done just like this:

$dadosEndereco = $request->only(['logradouro', 'numero'])

The result will be:

['logradouro' => 'Rua T', 'numero' => 116]  
  • In my all my models there is the $fillable varialvel with the respective table fields, I followed your tip and the controller update method looked like this: public Function update(Broker $broker, Request $request) { $documents = [ new App Document($dataDocument) ]; $addresses = [ new App Addressee($dataEndereco) ]; $broker->documents()->saveMany($documents); $broker->addresses()->saveMany($addresses); &##Xa;; Return redirect('brokers'); } All right? tested and the following error: Class 'App Http Controllers App Document' not found

  • Not every code posted here needs to be real. The idea of the code posted was not to apply a CTRL+C and CTRL+V, but to take a basis to solve your specific problem. After all, I’m only seeing snippets of code, and your script may have other dependencies.

Browser other questions tagged

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