0

I am presenting this error: ErrorException (E_NOTICE) Array to string conversion

This is the view from where I am sending the data from the inputs.

<div class="row">
@foreach ($candidates as $key => $candidate)
<input type="hidden" name="candidate_id[]" value="{{ $candidate->cdtid }}">
    <div class="col-12 col-md-6 col-xl-4">                                
        <div class="card">
            <div class="card-body" style="background-image: url('{{ URL::asset('img/politicparties/' . $candidate->ppbgi) }}');">
                <div class="form-group row">
                    <div class="col-md-6" style="text-align: left">
                        <img src="{{ URL::asset('img/politicparties/' . $candidate->ppimg) }}" alt="{{ $candidate->ppimg }}" width="40%" height="80%">                                                
                    </div>
                    <div class="col-md-6" style="text-align: right">
                        <img src="{{ URL::asset('img/candidates/' . $candidate->cdtav) }}" alt="image" width="50%" height="100%">                                        
                    </div>                                            
                </div>
                <div class="form-group row">
                    <div class="col-md-12">
                    <h5 class="card-title">{{ $candidate->ppn }}</h5>
                    </div>
                </div>   
                <div class="form-group row">
                    <div class="col-md-8">                   
                        <h5 class="card-title">{{ $candidate->cdtn }} {{ $candidate->cdtln }}</h5>
                    </div>
                    <div class="col-md-4">
                        <input type="number" class="form-control" id="InputNumber" name="total[]" value="0">
                    </div>
                </div>
            </div>                                
        </div>
        <br>                               
    </div>
@endforeach

This is the method in the controller.

/**
* Store a newly created resource in storage.
*
* @param  \Illuminate\Http\Request  $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
   $candidates = CandidatePoliticParty::candidates()->orderBy('id', 'ASC')->get();

   $procedings = ElectoralRecord::create([
       'electoralprecint_id'     => $request->get('electoralprecint_id'),
       'user_id'                 => $request->get('user_id'),
   ]);

   $inputs = $request->all();

   foreach ($inputs as $key => $value) {
       $votes = new VotingResult; 
       $votes->proceding_id              = $procedings->id;       
       $votes->candidate_id              = $request->input('candidate_id');
       foreach ($candidates as $key => $candidate) {
           $votes->politicparty_id           = $candidate->id;
       }            
       $votes->total                     = $request->input('total');
       $votes->save();
   };
}

Here I am trying to store the record id (proceding_id), the candidate id (candidate_id), the political party id (politicparty_id), and the value that is recorded in the form in the total (total) field.

The drawback I am presenting is that I am not correctly converting the array to the string generating the following error.

5
  • There is a lot of code here, is it all relevant to the particular problem or can you narrow it down to the code that is causing the problem. Commented May 15, 2020 at 5:48
  • Is $request->input('total') what you get for field with name total[] ? If so that's an array and may cause this issue. Same with candidate_id[]. There's also a lot of things that don't make sense like looping through $inputs instead of (possibly) looping through the input arrays e.g. total or candidate_id Commented May 15, 2020 at 6:34
  • I understand and thank you very much for the observation @Nigel Ren, that part but I am trying to put the necessary information to explain a little about the case, so that it can serve so that you can understand what I am trying to do and so you can guide me a little and tell me where I am making the mistake. Commented May 15, 2020 at 16:43
  • Thank you very much for the observation @apokryfos, in this case I am trying to send an array of with the value of each candidate in the form and store them in the database to get the result of the value of the records for each one, I am a little confused in as it should structure the way to insert them in the corresponding table. Commented May 15, 2020 at 16:44
  • There's lots of things you can improve here. Too many to make any sort of reasonable answer to this question (not one that could be generally useful to others as well). But one thing that stands out is the fact that user_id is a hidden field. A malicious user can just send a different id and there's no serverside verification going on at all. You should just use auth()->id() on the serverside instead of passing a hidden field. Commented May 15, 2020 at 20:18

1 Answer 1

1

I was organizing some things in the project and corrected the problem by doing a little research and was able to fix it as follows.

I hope it will be a useful resource for future cases.

$data = $request->all();

$proceding = $procedings->id;
$candidates = $data['candidate_id'];
$politicparties = $data['politiparty_id'];
$totals = $data['total'];

$rows = [];

foreach($candidates as $key => $input) {
    array_push($rows, [
        'proceding_id' => $proceding,
        'candidate_id' => isset($candidates[$key]) ? $candidates[$key] : '',
        'politicparty_id' => isset($politicparties[$key]) ? $politicparties[$key] : '',
        'total' => isset($totals[$key]) ? $totals[$key] : '' 
    ]);
}

VotingResult::insert($rows);
Sign up to request clarification or add additional context in comments.

Comments

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.