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.
$request->input('total')what you get for field with nametotal[]? If so that's an array and may cause this issue. Same withcandidate_id[]. There's also a lot of things that don't make sense like looping through$inputsinstead of (possibly) looping through the input arrays e.g.totalorcandidate_iduser_idis 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 useauth()->id()on the serverside instead of passing a hidden field.