数组到字符串转换插入到数据库

时间:2017-10-14 07:46:35

标签: arrays laravel

我有一个问题,数组再次串起来。但这次我需要插入数据库。

Checks::find($id)->update($request->all());
$issue[] = $request->issue;

foreach ($issue as $item) {
    dd($item);
    addIssues::create([
        'check_id' => $id,
        'issue_id' => $item
    ]);
}

迁移:

Schema::create('checks_issues', function (Blueprint $table) {
    $table->string('check_id');
    $table->string('issue_id');
    $table->timestamps();
    $table->primary(['check_id','issue_id']);
});

2 个答案:

答案 0 :(得分:0)

你应该试试这个:

Checks::find($id)->update($request->all());
    if(!empty($request->issue)){
       $issue = explode(',',$request->issue);

     foreach ($issue as $item){

        addIssues::create([
            'check_id' => $id,
            'issue_id' => $item
        ]);
     }
    }

答案 1 :(得分:0)

foreach ($issue as $item){
        foreach ($item as $item1){
            addIssues::create([
                'check_id' => $id,
                'issue_id' => $item1
            ]);
        }