简体   繁体   English

Laravel 中的 ErrorException (E_NOTICE) 数组到字符串的转换

[英]ErrorException (E_NOTICE) Array to string conversion in Laravel

I am presenting this error: ErrorException (E_NOTICE) Array to string conversion我出现这个错误: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.这是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.在这里,我尝试将记录 id (proceding_id)、候选人 id (candidate_id)、政党 id (politicparty_id) 以及表格中记录的值存储在总计 (total) 字段中。

The drawback I am presenting is that I am not correctly converting the array to the string generating the following error.我提出的缺点是我没有正确地将数组转换为生成以下错误的字符串。

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ErrorException (E_NOTICE) 未定义变量:用户 - ErrorException (E_NOTICE) Undefined variable: user ErrorException [注意]:数组到字符串转换PHP表单 - ErrorException [Notice]:Array to string conversion PHP Form ErrorException (E_NOTICE) 试图获取非对象的属性 - ErrorException (E_NOTICE) Trying to get property of non-object 数组中的未定义变量没有E_NOTICE吗? - No E_NOTICE for undefined variables in array? PHP公告– yii \\ base \\ ErrorException(数组到字符串的转换)Yii - PHP Notice – yii\base\ErrorException (Array to string conversion) Yii 带有转发器字段的 Laravel 表单提交中的 ErrorException 数组到字符串转换 - ErrorException Array to string conversion in Laravel form submission with repeater fields ErrorException 数组到字符串的转换 Laravel - 将 JSON 导入到 MySQL - ErrorException Array to string conversion Laravel - Importing JSON to MySQL ErrorException (E_NOTICE) 试图获取非对象的属性“default_locale”。 我似乎无法在这里找到问题 - ErrorException (E_NOTICE) Trying to get property 'default_locale' of non-object. I cannot seem to find the problem here 在 laravel 中上传图像时数组到字符串转换 ErrorException - Array to string conversion ErrorException while uploading image in laravel 7 PHP通知“ yii \\ base \\ ErrorException”,消息为“数组到字符串转换”-yii2 - PHP Notice 'yii\base\ErrorException' with message 'Array to string conversion' - yii2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM