Update several records at once

Asked

Viewed 1,643 times

1

Hello, I have a table with the following fields:KEY, VALUE , ALIAS, CLIENT_ID. In a certain system process I need to update Key and Value.

To update a record I have no problems, the problem is that when I need to change two KEYS and two VALUES of a client ID I am using the update function of the Laravel/repositories, then the code was like this:

 public function update(Request $request, $id)
 {
    $this->client->update($request->all(), $id);
 }

The problem is that with this method if I have 2 values and 2 Keys, it overrides the first so what I get is just

request = [ key => 'segundo valor do form', value => 'segundo valor do form'];

I need the 2 Keys and the 2 values. Can anyone help?

  • Using Repository? 2 values and 2 Keys from input? only if using array. Thus key[] =1&key[]=2

  • I did with Repository because it’s the only way I know how to do it, but if there’s another way, without using Repository, I can/can use it.

  • I only use Laravel without Repository. Respository is for something very specific, such as the need to change your Orm. I think Laravel’s ORM is already quite potent.

  • How the update would look using the ORM ?

  • This is actually the Controller’s method, right? Why are you passing Request. In fact, you need to explain how two values will come in Keys and Values. This comes from the form?

  • The normal ORM update is Cliente::where(['id' => $id])->update(['nome' => 'Nome', ' endereco' => 'Rua blablala']).

  • The form is built like this: {!! Form::open(array('method'=>'put', 'url' => url())) !! } @foreach() <div class="input-group" style="margin-top: 10px"> {!! Form::Hidden('key', $key) !! } {!! Form::text('value', '')) !! } <button> Submit</button> @endforeach {!! Form::close() !!}

  • @Ivanmoreira can you explain your problem better? do you want to update several lines of your database right? so are you getting more of an id or did I get the problem wrong? add more details and code please.

  • @Rafaelberro The problem has been fixed, I used the answer below and I foreach the controller to receive 1 or more keys.

  • @Ivanmoreira Great, you should select it as the solution to your question.

Show 5 more comments

1 answer

2


For you to submit a form with multiple values, you need to write it as follows, to facilitate data allocation:

{!! Form::open(array('method'=>'put', 'url' => url())) !!}

 @foreach($array as $key)
        {!! Form::text("clientes[$key][nome]", '')) !!}
        {!! Form::text("clientes[$key][endereco]", '')) !!}
        {!! Form::submit('Submit') !!}
 @endforeach 
{!! Form::close() !

!}

In the method responsible for saving the data, you could do so:

foreach  ($request->clientes as $id_key => $dados) {
    Cliente::where(['id' => $id_key])->update($dados);
}

Note that the way I declared the name of the above inputs, will cause PHP to generate a array thus:

[
    1 => ['nome' => 'Padaria Xodó', 'endereco' => 'Av Um - 155'],
    2 => ['nome' => 'Locadora Zezé', 'endereco' => 'Av Dois - 300']
]

Thus, internally, in the foreach, you will always update the data according to the id, which is the index of our array.

  • Thus, it says ->update($data) is a string and it has to receive an array

  • I would particularly select all clients in a single querie. For example, Client::wherein('id', array_keys($request->get('clients')));.

Browser other questions tagged

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