首页 > 解决方案 > 如何通过 user_id 更新批量记录?

问题描述

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>№&lt;/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,
        ]);
    }
}

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

标签: phplaraveleloquent

解决方案


添加$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,但是,您可以使用这样的

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


推荐阅读