【问题标题】:ErrorException (E_NOTICE) Array to string conversion in Laravel update methodLaravel更新方法中的ErrorException(E_NOTICE)数组到字符串转换
【发布时间】:2020-06-18 22:51:53
【问题描述】:

我正在尝试修复更新过程,我有以下模型属于与其他模型和表的关系:

protected $table = 'voting_results';

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

从表单中,我通过以下方式发送数据:

{!! 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() !!}

在控制器中,我正在接收并尝试更新表中的数据,如下所示:

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

}

但它返回 ErrorException (E_NOTICE) 数组到字符串的转换。

有人可以指导我,因为我已经尝试进行转换以存储数组数据。

对变量 $ 数据进行 dd,结果如下:

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"
  ]
]

这是一个架构和关系

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

【问题讨论】:

    标签: mysql laravel laravel-5.8 laravel-6 laravel-7


    【解决方案1】:

    在你有 update() 的地方删除‘array()’部分,因为你已经使用方括号‘[]’定义了一个数组

    可能值得将您的控制代码更改为以下内容:

    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!!!');
    }
    

    此外,您需要说明您拥有哪些列以及它们的用途。即我不知道total 应该包含整数还是字符串?它应该是您在$request-&gt;total 中的总数的总和吗?

    最好也使用$cast 来指示其中一些变量是什么,即:

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

    如果您将来必须对 total 列进行数学运算,那么最好将 total 的列类型更改为整数/双精度,或者如果您需要存储每个单独的 total 值以移动total 放到一个单独的表中。

    【讨论】:

    • 是的,我留下了,但我已经修改了它,在这种情况下它会更新第一条记录,其余的不会更新它们。
    • 我已经尝试通过 foreach 和不同的方法进行操作,但仍然返回相同的错误。
    • 你能做 dd($request->all()) 并发布结果吗?
    • 另外,你不应该对 $ids 和 $totals 使用 $data 变量而不是 $request 吗?
    • “总计”列应该包含什么?似乎您正在尝试将数据数组插入到单个列中,因此,您可以使用 implode - implode(',', $data['total']) 将总计转换为字符串。此外,如果您确实想存储数据数组,最好将数据存储为 JSON
    猜你喜欢
    • 1970-01-01
    • 2019-10-05
    • 2020-09-10
    • 2018-03-13
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    相关资源
    最近更新 更多