如何通过user_id更新大量记录?

时间:2018-06-07 06:14:03

标签: php laravel eloquent

Laravel如何通过user_id更新所有记录。我想通过该表单中的user_id更新所有记录。并且有像

这样的错误
  

缺少[Route:choices.update]

所需的参数

我的数据库

|----|---------|-------|-----------------|----------|
| id | user_id |  time | question_number | topic_id |
|----|---------|-------|-----------------|----------|
| 1  |    1    | 60:00 |        10       |    1     |
|----|---------|-------|-----------------|----------|
| 2  |    1    | 60:00 |        5        |    2     |
|----|---------|-------|-----------------|----------|
.....................................................
| 10 |    2    | 30:00 |        5        |    1     |
|----|---------|-------|-----------------|----------|
| 11 |    2    | 30:00 |        7        |    2     |
|----|---------|-------|-----------------|----------|

尝试像这样的更新

|----|---------|-------|-----------------|----------|
| id | user_id |  time | question_number | topic_id |
|----|---------|--------|-----------------|----------|
| 1  |    1    | 120:00 |        25       |    1     |
|----|---------|--------|-----------------|----------|
| 2  |    1    | 120:00 |        4        |    2     |
|----|---------|--------|-----------------|----------|
......................................................
| 10 |    2    | 90:00  |       15        |    1     |
|----|---------|--------|-----------------|----------|
| 11 |    2    | 90:00  |       19        |    2     |
|----|---------|----=---|-----------------|----------|

查看

<form method="post" action="{{ route('choices.update') }}">
{{ csrf_field() }}
<table class="table table-bordered table-striped">
        <thead>
        <tr class="info">
        <th>№</th>
        <th>Нэр</th>
        <th>Сэдэв</th>
        <th>Асуултын тоо</th>
        </tr>
        </thead>
        <tbody>
        @foreach($choices as $choice)
        <tr>
        <td>{{ $choice->id }}</td>
        <td>{{ Auth::user()->name }}</td>
        <td>{{ $choice->topic->title }}</td>
        <td><input value="{{ $choice->question_number }}"
                   name="number[{{ $choice->id }}]" step="1" min="0"></td>
        </tr>
        @endforeach
        </tbody>
        </table>
        <a href="#" class="btn btn-primary" type="submit">Хадгалах</a>
</form>

路线

   |        | GET|HEAD  | choices                                   | choices.index                  | App\Http\Controllers\ChoicesController@index                           | web,auth                                     |
   |        | POST      | choices                                   | choices.store                  | App\Http\Controllers\ChoicesController@store                           | web,auth                                     |
   |        | GET|HEAD  | choices/create                            | choices.create                 | App\Http\Controllers\ChoicesController@create                          | web,auth                                     |
   |        | DELETE    | choices/{choice}                          | choices.destroy                | App\Http\Controllers\ChoicesController@destroy                         | web,auth                                     |
   |        | PUT|PATCH | choices/{choice}                          | choices.update                 | App\Http\Controllers\ChoicesController@update                          | web,auth                                     |
   |        | GET|HEAD  | choices/{choice}                          | choices.show                   | App\Http\Controllers\ChoicesController@show                            | web,auth                                     |
   |        | GET|HEAD  | choices/{choice}/edit                     | choices.edit                   | App\Http\Controllers\ChoicesController@edit                             | web,auth                                     |

控制器

public function update(Request $request, $id){
    foreach ($request->input('number') as $key => $value) {
        Choice::where('user_id',Auth::id())->update([
            'user_id' => Auth::id(),
            'time'  => $time,
            'topic_id' => $key,
            'question_number' => $value,
        ]);
    }
}

然后我要写一些东西来提供我的代码。但我不知道怎么做。

1 个答案:

答案 0 :(得分:0)

$choice->id添加到路线:

{{ route('choices.update', $choice->id) }}

这应该摆脱Missing required parameters

至于进行“批量更新”,我建议创建一条新路线,因为当前路线应该用于更新单个choice,例如

Route::put('user/choices', ...) //Obviously, you change "put" to be post or patch if you want.

Laravel目前(据我所知)支持ON DUPLICATE KEY,但是,您可以使用this这样的包。

另外,在您的表单中,您使用选项ID为number设置了密钥,但是在您的控制器中,您正在使用主题ID的密钥,我不确定是否正确。