如何从单一表单在数据库中添加多行?

时间:2019-02-06 15:55:30

标签: laravel laravel-5.7

我正在使用Laravel设置一个名册Web应用程序,并且我需要一个表单,用于输入所有字段,然后将它们存储到数据库中,当我使用修补程序或提交单行时,一切工作正常,但是当我尝试提交整个表单时,仅读取最后一行。

这是rota.blade.php文件

{!! Form::open(['action' => 'RotaController@store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}

<table class="table table-striped table-bordered">
    <thead class="thead-dark">
        <tr>
            <th scope="row">Name</th>
            <th scope="row">Sunday</th>
            <th scope="row">Monday</th>
            <th scope="row">Tuesday</th>
            <th scope="row">Wednesday</th>
            <th scope="row">Thursday</th>
            <th scope="row">Friday</th>
            <th scope="row">Saturday</th>
        </tr>
    </thead>
    <tbody>
    <!--
        * create a loop to iterate through each instance in the weekly rota database
        * and create a row for each one; in case the username coincides with the name in the rota
        * add class="active" to each row, so it is easier for the user to identify its own row
    -->
        @csrf
        @foreach ($users as $user)
            <tr>
                <th scope="row">
                    {{$user->first_name}}
                    <input name="user_id" type="hidden" value="{{$user->id}}">
                    <input name="week_no" type="hidden" value="{{$currWeek}}">
                </th>
                <th scope="row">
                    {{Form::text('sunday', '', ['class' => 'form-control'])}}
                </th>
                <th scope="row">
                    {{Form::text('monday', '', ['class' => 'form-control'])}}
                </th>
                <th scope="row">
                    {{Form::text('tuesday', '', ['class' => 'form-control'])}}
                </th>
                <th scope="row">
                    {{Form::text('wednesday', '', ['class' => 'form-control'])}}
                </th>
                <th scope="row">
                    {{Form::text('thursday', '', ['class' => 'form-control'])}}
                </th>
                <th scope="row">
                    {{Form::text('friday', '', ['class' => 'form-control'])}}
                </th>
                <th scope="row">
                    {{Form::text('saturday', '', ['class' => 'form-control'])}}
                </th>   
            </tr>
        @endforeach
    {{Form::submit('Submit', ['class' => 'btn btn-primary'])}}
</tbody>

</table>    
{!! Form::close() !!}

这是RotaController@store

public function store(Request $request)
{
     //validation
     $this->validate($request,[
        'user_id' => 'required',
        'sunday' => 'required',
        'monday'=> 'required',
        'tuesday'=> 'required',
        'wednesday'=> 'required',
        'thursday'=> 'required',
        'friday'=> 'required',
        'saturday'=> 'required',
        'week_no'=> 'required'
    ]);

    //create a field
    $rota = new Rota;
    $rota->user_id = $request->input('user_id');
    $rota->week_no = $request->input('week_no');
    $rota->sunday = $request->input('sunday');
    $rota->monday = $request->input('monday');
    $rota->tuesday = $request->input('tuesday');
    $rota->wednesday = $request->input('wednesday');
    $rota->thursday = $request->input('thursday');
    $rota->friday = $request->input('friday');
    $rota->saturday = $request->input('saturday');
    $rota->save();

    $users = DB::table('users')->orderBy('first_name', 'asc')->get();
    return view('admin.admin-rota')->with('users', $users);

}

我尝试了dd($request),实际上它仅获取最后一组输入字段, 我想我必须遍历它们,但我不知道如何。有人对尝试什么有任何建议吗?非常感谢您的任何反馈,但使用此框架仍然很新。

2 个答案:

答案 0 :(得分:0)

我将按用户对表单数据进行分组。

    @foreach ($users as $user)
                <tr>
                    <th scope="row">
                        {{$user->first_name}}
                        <input name="users[{{$user->id}}][id]" type="hidden" value="{{$user->id}}">
                        <input name="users[{{$user->id}}][week_no]" type="hidden" value="{{$currWeek}}">
                    </th>
                    <th scope="row">
                        {{Form::text("users[$user->id][sunday]", '', ['class' => 'form-control'])}}
<th scope="row">
                        {{Form::text("users[$user->id][monday]", '', ['class' => 'form-control'])}}

                </tr>
            @endforeach
        {{Form::submit('Submit', ['class' => 'btn btn-primary'])}}
    </tbody>

在控制器中,您将拥有一个数组,其中包含每个用户的数据。 然后您的控制器可能如下所示:

 public function store(Request $request)
{
     //validation
     $this->validate($request,[
        'users.*.user_id' => 'required',
        'users.*.sunday' => 'required',
        ...
    ]);
    foreach($request->get('users') as $userData){
       $rota = new Rota;
       $rota->user_id = $userData['user_id'];
       $rota->week_no = $userData['week_no'];
       $rota->sunday = $userData['sunday'];
       ...
       $rota->save();
    }
    //create a field


    $users = DB::table('users')->orderBy('first_name', 'asc')->get();
    return view('admin.admin-rota')->with('users', $users);
}

答案 1 :(得分:0)

您可以在每个输入的名称后附加一个索引,并保留一个隐藏的输入以存储收集计数。

@php
    $idx = 0;
@endphp

@foreach ($users as $user)
<tr>
    <th scope="row">
        {{$user->first_name}}
        <input name="user_id_{{$idx}}" type="hidden" value="{{$user->id}}">
        <input name="week_no_{{$idx}}" type="hidden" value="{{$currWeek}}">
    </th>
    <th scope="row">
        {{Form::text('sunday_' . $idx , '', ['class' => 'form-control'])}}
    </th>
    <th scope="row">
        {{Form::text('monday_' . $idx, '', ['class' => 'form-control'])}}
    </th>
    <th scope="row">
        {{Form::text('tuesday_' . $idx, '', ['class' => 'form-control'])}}
    </th>
    <th scope="row">
        {{Form::text('wednesday_' . $idx, '', ['class' => 'form-control'])}}
    </th>
    <th scope="row">
        {{Form::text('thursday_' . $idx, '', ['class' => 'form-control'])}}
    </th>
    <th scope="row">
        {{Form::text('friday_' . $idx, '', ['class' => 'form-control'])}}
    </th>
    <th scope="row">
        {{Form::text('saturday_' . $idx, '', ['class' => 'form-control'])}}
    </th>   

    @php 
        $idx++;
    @endphp
</tr>
@endforeach
{{-- holds numbers of users count --}}
<input type="hidden" name="user_count" value="{{$users->count()}}"> 

在控制器中,使用从刀片文件中获取的变量$user_count遍历用户的数量。每个数据变量将具有附加的索引,然后您就可以对应于从刀片文件输入的正确数据。注意,这里的验证是在每次迭代的每个对象上完成的。如果您想在保存之前验证所有数据,请在保存数据之前再进行一次foreach验证。

for ($i=0; $i < $request->user_count; $i++) { 
       //validation
    $this->validate($request,[
        'user_id_' . $i => 'required',
        'sunday_' . $i => 'required',
        'monday_'  . $i=> 'required',
        'tuesday_'  . $i => 'required',
        'wednesday_'  . $i=> 'required',
        'thursday_'  . $i => 'required',
        'friday_'  . $i=> 'required',
        'saturday_' . $i=> 'required',
        'week_no_' . $i=> 'required'
    ]);

    //create a field
    $rota = new Rota;
    $rota->user_id_{$i} = $request->input('user_id_' . $i);
    $rota->week_no_{$i} = $request->input('week_no_' . $i);
    $rota->sunday_{$i} = $request->input('sunday_' . $i);
    $rota->monday_{$i} = $request->input('monday_' . $i);
    $rota->tuesday_{$i} = $request->input('tuesday_' . $i);
    $rota->wednesday_{$i} = $request->input('wednesday_' . $i);
    $rota->thursday_{$i} = $request->input('thursday_' . $i);
    $rota->friday_{$i} = $request->input('friday_' . $i);
    $rota->saturday_{$i} = $request->input('saturday_' . $i);
    $rota->save();
}