1

I'm trying to save a relationship the way I saw on the docs, but isn't working.

On my Contato (Contact) model I've:

public function filhoContato()
{
    return $this->hasMany('FilhoContato', 'id_contato');
}

Along with my fillables to enable mass-assignment

My FilhoContato (ContactChildren) model:

public function contato()
{
    return $this->belongsTo('Contato');
}

And on my controller:

$contato = Contato::create(array(
        'nome' => Input::get('nome'),
        'nascimento' => $data,
        'cpf' => Input::get('cpf'),
        'tel_principal' => Input::get('telefone'),
        'idade' => Input::get('idade'),
        'email' => Input::get('email'),
        'tipo_end' => Input::get('tipo'),
        'cep' => Input::get('cep'),
        'estado' => Input::get('estado'),
        'cidade' => Input::get('cidade'),
        'bairro' => Input::get('bairro'),
        'rua' => Input::get('rua'),
        'numero' => Input::get('numero'),
        'logradouro' => Input::get('logradouro'),
        'genero' => Input::get('genero'),
        'estadoCivil' => Input::get('estadoCivil'),
        'mae' => Input::get('mae'),
        'pai' => Input::get('pai'),
        'filhos' => Input::get('filhos'),
        'grupo' => Input::get('grupo'),
        'caminho' => $filename . $extension,
        'ativo' => Input::get('ativo'),
        'exaluno' => Input::get('exaluno')
    ));


    $filhocontato = new FilhoContato(array('nome' => Input::get('name')));

    $contato = Contato::find(1);

    $filhocontato = $contato->filhoContato()->save($filhocontato);

However only the Contato (contact) table data gets inserted.

How can I save both tables at same time? I need a loop to get all value from Input::get('name') too.

Just to clarify, if my Contato(contact) has a children then he will insert their names in a dynamically generated form text field and when he clicks submit all of his data will go to Contato(contact) table and his children (if he has) will go to filhocontato table which structure is (id, nome, id_contato). That's what I'm aiming for at least :/

SylaQo
  • 21
  • 3
  • Hmm the save call on the relation looks right to me... Shouldn't you use the `$contato` variable of the model you just created instead of using `find(1)` though? – lukasgeiter Dec 14 '14 at 15:38

0 Answers0