Skip to content
Advertisement

ErrorException (E_NOTICE) Array to string conversion in Laravel

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  IlluminateHttpRequest  $request
* @return IlluminateHttpResponse
*/
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.

Advertisement

Answer

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);
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement