数据未正确存储在数据库中,laravel、php

the data did not store in the database correctly, laravel, php

在我的项目中,我想让学生select标题,然后title-related信息将存储在数据库中。但是,当我使用模式时,只有 preferenceOreder 被正确存储。 user_id 和标题 ID 错误;

这是我的控制器:$title 有 属性 user_id 和 id

 public function titleSelect(Title $title, Request $request)
    {

        ApplicationStudent::firstOrcreate([
            'user_id'=>Auth::id(),
            'supervisor_id'=>$title->user_id,
            'title_id'=>$title->id,
            'preferenceOrder' => request('preferenceOrder')
        ]);
        return redirect('student/titleIndex');
    }

那么,这是我的blade.php

@foreach($titles as $title)
<td>

                        <!-- Modal -->
                        <div class="modal fade" id="applyModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="exampleModalLabel">Apply topic</h5>
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">&times;</span>
                                        </button>
                                    </div>
                                    <form class="form-horizontal" method="post" action="/title/{{$title->id}}/select">
                                        @csrf

                                        <div class="modal-body">
                                            <input type="hidden" name="title_id" id="titleMk">
                                            <div class="form-check-inline">
                                                <label for="titleMark1">
                                                    <input class="form-check-input" type="radio" name="preferenceOrder" id="preferenceOrder" value="1"  checked @if($apply->has(1)) disabled @endif>
                                                    <label class="form-check-label" for="titleMark1">First choice</label>
                                                </label>
                                            </div>
                                            <br/>

                                            <div class="form-check-inline">
                                                <label for="titleMark2">
                                                    <input class="form-check-input" type="radio" name="preferenceOrder" id="preferenceOrder" value="2" checked @if($apply->has(2)) disabled @endif>
                                                    <label class="form-check-label" for="titleMark2">Second Choice</label>
                                                </label>
                                            </div>
                                            <br/>

                                            <div class="form-check-inline">
                                                <label for="titleMark3">
                                                    <input class="form-check-input" type="radio" name="preferenceOrder" id="preferenceOrder" value="3" checked @if($apply->has(3)) disabled @endif>
                                                    <label class="form-check-label" for="titleMark3">Third Choice</label>
                                                </label>
                                            </div>
                                            <br/>
                                        </div>

                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                            <button type="submit" class="btn btn-primary">Save changes</button>
                                        </div>
                                    </form>

                                </div>
                            </div>
                        </div>
                        @if(!$title->titleSelection(Auth::id())->exists() && $apply->count() < 3)
                        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#applyModal" >
                            Apply
                        </button>
                            @endif
                    </td>
@endforeach

这是我的路线:

Route::post('/title/{title}/select','StudentController@titleSelect');

问:user_id和title id错误;

甲:

这是在 Foreach 中使用模态时的常见错误..

对于模态,id 应该是唯一的,对于上面的模态,所有模态的 id 都是 id="applyModal"

将加载第一个值并在之后重复。不会填充新值

通过添加唯一 ID,模态将填充相应的结果..

注意:html页面中不能没有相似的ID。它会给你一个错误..

尝试更新它

<!-- Modal -->

<div class="modal fade" id="applyModal-{{$title->id}}" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
....
</div>

调用模式时应使用相同的 ID..

我就是这样

@foreach($title as $title)

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal-{{$title->id}}">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal-{{$title->id}}" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
</div

@endforeach

对于发布答案而不是评论,我深表歉意,但我无法发表评论或 up-vote

但是,您也应该在模型上使用 foreach。正如 mightyteja 所建议的那样。

如果你想要更健壮和 javascript 的东西,我建议使用 Livewire,它实际上很容易使用

另一个注意事项。我建议为您的路线命名!

例如:

Route::post('/title/{title}/select','StudentController@titleSelect')->name('student.title.select');

现在在您的视图中,您可以简单地执行以下操作:

<form class="form-horizontal" method="post" action="{{ route('student.title.select', $title->id) }}">

或者对于多个 uri 变量:

{{ route('name', ['id' => $model->id, 'name' => $model->name]) }}

我推荐这个,因为如果出于某种原因你改变了路线。您不必在视图中的任何地方更改它(更易于维护)。命名功能其实很神奇