Difficulties creating update with Laravel 5.1?

Asked

Viewed 2,555 times

1

I’m trying to create a update with Laravel 5.1 but is bringing the following error:

Type error: Argument 1 passed to Illuminate\Database\Eloquent\Builder::update() must be of the type array, object given

Follows my Controller

public function update($id)
{

                $proposta = $this->proposta;

        $proposta->cliente_id = $this->request->get('cliente_id');
        $proposta->contato = $this->request->get('contato');
        $proposta->email = $this->request->get('email');
        $proposta->telefone = $this->request->get('telefone');
        $proposta->fatcnpj = $this->request->get('fatcnpj');
        $proposta->atendimento = $this->request->get('atendimento');
        $proposta->dt_solicitacao = $this->request->get('dt_solicitacao');
        $proposta->dt_vigencia = $this->request->get('dt_vigencia');
        $proposta->vendedor = $this->request->get('vendedor');
        $proposta->coleta = $this->request->get('coleta');
        $proposta->dt_integracao = $this->request->get('dt_integracao');
        $proposta->hr_integracao = $this->request->get('hr_integracao');
        $proposta->frete_material = $this->request->get('frete_material');
        $proposta->status_id = $this->request->get('status_id');  

        $this->proposta->where('id', $id)->update($dadosForm);

        $dadosForm = $this->request->except('_token');
        $proposta_id = $id;

        $count = $this->ensaios->max('id');


        for($i=1;$i<=$count;$i++){ //Save Ensaios

        $proposta_ensaios = new PropostaEnsaios();

        $proposta_ensaios->id_proposta = $proposta_id;
        $proposta_ensaios->id_produto = $i;
        $proposta_ensaios->quantidade = $dadosForm['quantidade_'.$i];
        $proposta_ensaios->valor= $dadosForm['valor_'.$i];
        $proposta_ensaios->total = $dadosForm['total_'.$i];

        $proposta_ensaios->where('id', $id)->update($dadosForm);
}
  • in which of the two updates exactly happens this?

  • Don’t you just save? in $proposta->save() ????

  • @Neuberoliveira in the First but the second is the same will happen the same thing

  • @Virgilionovic the $proposta->save() it try to give an Insert and not an update

  • @Shaolinfantastic ai has for minors, I can even feel new instance make her update instead of Insert, of course I understand what you said because I would do totally different bringing the direct instance of research with a find(1) and then changed, so your code just needs to be tidied up to solve your problem is that I’m sorry I didn’t find the fil of the skein !

  • Example here I would do: $proposta = $this->proposta->find(1);; is already in update mode changing the fields and giving a $proposta->save(); I believe it solves that part of the problem! @Shaolinfantastic

  • 1

    @Virgilionovic I believe the update works because the $id ends up being null, ai when it arrives at the bank, probably the id is auto increment, then when it receives null continues the sequence

  • @Neuberoliveira did not understand what you just wrote as a comment.

  • Something that can work too, for a little while I do not move with Aravel, since it is populating the object before, try so, $this->proposta->where('id', $id)->update()

Show 4 more comments

1 answer

2

Update parameter needs to be an array chave=>valor That’s why you’re making the mistake.

The second update will probably work because the $this->request->except('_token'); will return an array chave=>valor with respect to the field _token.

But back in doubt I think the right thing would be something more like this

$dadosPropostaForm = $this->request->except('_token');
$this->proposta->where('id', $id)->update($dadosPropostaForm);

No how to save the essays you also need an array chave=>valor

$proposta_ensaios = new PropostaEnsaios();

$proposta_ensaios_dados['id_proposta'] = $proposta_id;
$proposta_ensaios_dados['id_produto'] = $i;
$proposta_ensaios_dados['quantidade'] = $dadosForm['quantidade_'.$i];
$proposta_ensaios_dados['valor']= $dadosForm['valor_'.$i];
$proposta_ensaios_dados['total'] = $dadosForm['total_'.$i];

$proposta_ensaios->where('id', $id)->update($proposta_ensaios_dados);
  • Only a doubt in this example would have to change my Model?

  • no, the problem is that update takes an array as parameter and not an object. As I said the request will return an array, and in the trials I just formatted the data as an array

  • The same error appears from the beginning Type error: Argument 1 passed to Illuminate Database Eloquent Builder::update() must be of the type array, null Given, called

  • check what is coming in $dadosPropostaForm what happens now is that for some reason this going null in the parameter of update().

  • It didn’t work this way but it didn’t work

Browser other questions tagged

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