Update with hasMany relationship for two inputs at the same time in laravel

0
Well the problem that happens is the following, I have in my application the field phone that is present in a relationship to personFisica then a person has a relationship hasMany for phones and that in turn has a belongto for good person up there okay the problem is happening when I make the change since there are two inputs, the code of the view looks like this:

 @foreach($pessoaFisica->telefones as $tel)
   <div class="col-sm-6">
   <label for="telefone" class="col-sm-6 control-label">Telefone:</label>
   <input type="tel" class="form-control" name="numero[]" id="numero"
   placeholder="Telefone - obrigatório"
   value="{{isset($tel->numero) ? $tel->numero : null}}">
   </div>
 @endforeach

This gives you a list of phones as follows:

And now I'm trying to get the two fields to edit in the table, and I'm trying to do this as follows:

 $dados = $request->input('numero');
 $this->telefone->where('id_pessoa', $idPessoa)->update($dados);

But an error is occurring. I know this is happening because it is a list but I do not know how to insert it in bulk at one time or another.

    
asked by anonymous 25.07.2016 / 06:33

1 answer

-1

The update method expects an associative array as a parameter. Take a look at the documentation: link

So the correct method of doing the update is:

$dados = $request->only('numero'); // array ['numero' => '(number)']
$this->telefone->where('id_pessoa', $idPessoa)->update($dados);

Remembering that to create and bulk update you need to define in the model.

  

You can also use the create method to save a new model in a single   line. The inserted model instance will be returned to you from the   method. However, before doing so, you will need to specify either   fillable or guarded attribute on the model, all Eloquent models   protect against mass-assignment.

    
25.07.2016 / 13:53