0

I am trying to fix an update process, I have the following model that has belongs to relationship with other models and tables:

protected $table = 'voting_results';

protected $fillable = ['proceding_id', 'candidate_id', 'politicparty_id', 'total'];

From the form, I am sending the data in the following way:

{!! Form::model($electoralrecord, ['method' => 'PATCH','route' => ['electoralrecords.update', $electoralrecord->id], 'enctype' => 'multipart/form-data']) !!}

    <input type="number" class="form-control" id="total" name="total[]" value="{{ $candidate->total }}" placeholder="Values">

    <button class="btn btn-success" type="submit" data-toggle="tooltip" rel="tooltip" data-placement="top" title="Guardar cambios"><i data-feather="save"></i> Save</button>

{!! Form::close() !!}

In the controller I am receiving and trying to update the data in the table like this:

public function update(Request $request, $id)
{

    $data = $request->all(); 

    $ids = $request->id[0];
    $totals = $request->total[0];

    DB::table('voting_results')->where('id', $ids)
        ->update(array(['total' => $totals]));

    return redirect()->route('electoralrecords.index')
                    ->with('success','Record update!!!');

}

But it returns ErrorException (E_NOTICE) Array to string conversion.

Someone who can guide me, because I have tried to do the conversion to store the array data.

Making a dd of the variable $ data, this is the result:

array:4 [▼
  "_method" => "PATCH"
  "_token" => "dHZlKbY1x2xOteQ31pPIMkmwQc3BfHIRWrXlKU87"
  "id" => "1"
  "total" => array:27 [▼
    0 => "12"
    1 => "10"
    2 => "0"
    3 => "0"
    4 => "0"
    5 => "0"
    6 => "0"
    7 => "0"
    8 => "0"
    9 => "0"
    10 => "0"
    11 => "0"
    12 => "0"
    13 => "0"
    14 => "0"
    15 => "0"
    16 => "0"
    17 => "0"
    18 => "0"
    19 => "0"
    20 => "0"
    21 => "0"
    22 => "0"
    23 => "0"
    24 => "0"
    25 => "0"
    26 => "4"
  ]
]

This is a Schema and relationships

Schema::create('voting_results', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('proceding_id')->unsigned()->nullable();
    $table->foreign('proceding_id')
        ->references('id')
        ->on('proceedings')
        ->onDelete('cascade');
    $table->integer('candidate_id')->unsigned()->nullable();
    $table->foreign('candidate_id')
        ->references('id')
        ->on('candidates')
        ->onDelete('cascade');
    $table->integer('politicparty_id')->unsigned()->nullable();
    $table->foreign('politicparty_id')
        ->references('id')
        ->on('politicparty')
        ->onDelete('cascade');
    $table->string('total');
    $table->timestamps();
});

1 Answer 1

1

Where you have update() delete ‘array()’ part, since you have already defined an array using brackets ‘[]’

It might be worth changing your control code to the following:

public function update(Request $request, $id)
{
    $data = $request->all(); 
    $totals = implode(',', $data['total']);

    VotingResult::where('id', $data['id'])
        ->update(array(['total' => $totals]));

    return redirect()->route('electoralrecords.index')
                    ->with('success','Record update!!!');
}

Also, you need to explain what columns you have and what they are for. i.e. I dont know if total should contain an integer or a string? should it be a total sum of totals that you have in the $request->total?

It would be good to also use $cast to indicate what some of those variables are, i.e.:

class VotingResult extends Model
{
   protected $cast = [
      'total' => 'json',
      'proceding_id' => 'integer',
   ];
}

If you have to do mathematical operations on the total column in the future then you might be better off changing column type of total to integer/double or if you need to store every single total value to move total into a separate table.

Sign up to request clarification or add additional context in comments.

24 Comments

Yes, I had stayed but I already modified it, in this case it updates the first record, the rest does not update them.
I have tried doing it through foreach and different methods, but it still returns the same error.
Could you do dd($request->all()) and post results?
Also, shouldn’t you be using $data variable rather than $request for $ids and $totals?
What should ‘total’ column contain? Seems like you are trying to insert an array of data into a single column, so, you can turn totals into a string using implode - implode(‘,’, $data[‘total’]). Also, if you do want to store an array of data you might be better off storing data as a JSON
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.