【问题标题】:Laravel 5.0: How to update an array of data: preg_replace(): Parameter mismatch, pattern is a string while replacement is an arrayLaravel 5.0:如何更新数据数组:preg_replace():参数不匹配,模式是字符串,而替换是数组
【发布时间】:2016-04-20 08:23:11
【问题描述】:

我是 laravel 的绝对初学者。 当我尝试更新数据数组时,我正在处理错误“preg_replace(): Parameter mismatch, pattern is a string while replacement is an array”

有人知道解决这个错误的方法吗? 如果您需要更多信息,请留下您的 cmets。

任何建议将不胜感激。提前致谢!

LogsController.php

public function update(CreateLogRequest $request, $course_id){

    $count = count($request->input('weeks'));
    $input = $request->all();
    $logs = array();

    for ($i = 0; $i < $count; $i++){
        if($input['weeks'][$i]){
            array_push($logs, array(
                'user_id' => \Auth::user()->id,
                'course_id' => $course_id,
                'weeks' => $i + 1,
                'work_description' => $input['work_description'][$i],
                'instructor_comments' => $input['instructor_comments'][$i],
                'status' => $input['status'][$i],
                'created_at' => Carbon::now(),
                'updated_at' => Carbon::now(),
            ));
        }
    }
    $log->update($logs);
    return redirect('/student/home');
}

当我输入一个代码 dd($logs) 时,结果如下。

array:2 [▼
  0 => array:8 [▼
  "user_id" => "1"
  "course_id" => "39"
  "weeks" => 1
  "work_description" => "fadfad"
  "instructor_comments" => "fdasfda"
  "status" => "accepted"
  "created_at" => Carbon {#219 ▶}
  "updated_at" => Carbon {#212 ▶}
]
 1 => array:8 [▼
   "user_id" => "1"
   "course_id" => "39"
   "weeks" => 2
   "work_description" => "fadsfad"
   "instructor_comments" => "fdasfdasfad"
   "status" => "accepted"
   "created_at" => Carbon {#218 ▶}
   "updated_at" => Carbon {#222 ▶}
 ]
]  

Log_edit.blade.php

{!! Form::hidden('course_id', $course->id) !!}
    @foreach($logs as $log)
        <tbody>
        <tr>
            <td>
                {{ $log->weeks }}
                {!! Form::hidden('weeks[]', $log->weeks) !!}
            </td>

            <td> {!! Form::textarea('work_description[]', $log->work_description) !!}  </td>

            <td> {!! Form::textarea('instructor_comments[]', $jlog->instructor_comments) !!} </td>

            <td> {!! Form::select('status[]',
                        array('accepted' => 'accepted',
                              'pending' => 'pending',
                              'declined' => 'declined',
                      ), $log->status) !!}
            </td>
        </tr>
        @endforeach

        </tbody>

【问题讨论】:

  • 周数是否与日志存储在单独的表中?
  • 不,同一张桌子

标签: php laravel laravel-5


【解决方案1】:

问题来了:

$log->update($logs);

update 方法不采用多维数组。

您的LogsController.php 应该是这样的:

public function update(CreateLogRequest $request, $course_id, Log $log){

    $input = $request->all();

    foreach ($input['weeks'] as $i => $log_id){
        $data = [
          'user_id' => \Auth::user()->id,
          'course_id' => $course_id,
          'weeks' => $i + 1,
          'work_description' => $input['work_description'][$i],
          'instructor_comments' => $input['instructor_comments'][$i],
          'status' => $input['status'][$i],
          'created_at' => Carbon::now(),
          'updated_at' => Carbon::now(),
        ];

        $log->where('id', $log_id)->update($data);
    }

    return redirect('/student/home');
}

【讨论】:

  • 非常感谢您告诉我问题所在!但是,您显示的代码不起作用...
  • 感谢您的更新!它以某种方式工作,但不是我希望代码工作的方式。
  • "foreach ($input['weeks'] as $i => $log_id)", "$log->where('id', $log_id)->update($data); "由于代码中设置了“log_id”,代码会更新前几对日志。
【解决方案2】:

经过一些思考和改变,一切都按照我想要的方式进行。非常感谢 CharlieJade!

LogsController.php

public function update(CreateJournalRequest $request, $course_id){

    $input = $request->all();

    foreach ($input['id'] as $i => $log_id){
        $data = [
            'user_id' => \Auth::user()->id,
            'course_id' => $course_id,
            'weeks' => $i + 1,
            'work_description' => $input['work_description'][$i],
            'instructor_comments' => $input['instructor_comments'][$i],
            'status' => $input['status'][$i],
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now(),
        ];

        Journal::where('id', $log_id)->update($data);
    }

    return redirect('/student/home');
 }

Logs_edit.blade.php

  @foreach($logs as $log)
        <tbody>
        {!! Form::hidden('id[]', $log->id) !!}
        <tr>
            <td>
                {{ $log->weeks }}
                {!! Form::hidden('weeks[]', $log->weeks) !!}
            </td>

            <td> {!! Form::textarea('work_description[]', $log->work_description) !!}  </td>

            <td> {!! Form::textarea('instructor_comments[]', $log->instructor_comments) !!} </td>

            <td> {!! Form::select('status[]',
                        array('accepted' => 'accepted',
                              'pending' => 'pending',
                              'declined' => 'declined',
                      ), $log->status) !!}
            </td>
        </tr>
        @endforeach
        </tbody>

【讨论】:

    猜你喜欢
    • 2017-03-26
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    • 2015-06-19
    • 2019-05-18
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多