【问题标题】:How To submit form by one submit button in foreach loop in laravel?如何在 laravel 的 foreach 循环中通过一个提交按钮提交表单?
【发布时间】:2021-06-29 06:06:50
【问题描述】:

这是我的创建页面。这里有提交按钮的 foreach 循环中的表单。 (这是我的创建页面。这里有 foreach 循环中的表单,带有提交按钮。)

<form method="post" action="{{route('types.store')}}">
        @csrf
        <table>
            @foreach ($students as $student)

                <tr>
                    <td>Name : </td>
                    <td><input type="text" class="form-control" name="name" value="{{ $student->name }}" readonly></td>
                </tr>
                <tr>
                    <td>Type : </td>
                    <td>
                        <select name="type" id="type">
                            <option value="present">present</option>
                            <option value="absent">absent</option>
                            <option value="half_day">half day</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Date : </td>
                    <td><input type="date" class="form-control" name="date"></td>
                </tr>

            @endforeach
            <button class="btn btn-primary" name="submit" id="submit">Submit</button>
        </table>
    </form>

这是我的类型控制器。这是我的类型控制器。我想将数据存储在 foreach 循环中。 (这是我的 TypeController。这是我的 TypeController。我想在 foreach 循环中存储数据)

public function create()
{
    $students = Student::all();
    return view('types.create', compact('students'));
}
public function store(Request $request)
{
    $request->validate([
        'name' => 'required',
        'type' => 'required',
        'date' => 'required',
    ]);

    $type = Type::create([
        'name' => $request->input('name'),
        'type' => $request->input('type'),
        'date' => $request->input('date'),
    ]);

    return redirect()->route('types.index')->withSuccess('Done');
}

这是我的 Type 模型,与 Student 表有关系(这是我的 Type 模型,与 Student 表有关系)

use HasFactory;

protected $table = 'types';

protected $fillable = [
    'name',
    'date',
];

public function student()
{
    return $this->belongsTo(Student::class);
}

【问题讨论】:

  • 我认为你可以将整个表单放在 foreach 中

标签: laravel laravel-5 eloquent laravel-4 laravel-8


【解决方案1】:

为每个输入创建数组,如下所示:

<form method="post" action="{{route('types.store')}}">
        @csrf
        <table>
            @foreach ($students as $student)

                <tr>
                    <td>Name : </td>
                    <td><input type="text" class="form-control" name="name[]" value="{{ $student->name }}" readonly></td>
                </tr>
                <tr>
                    <td>Type : </td>
                    <td>
                        <select name="type[]" id="type">
                            <option value="present">present</option>
                            <option value="absent">absent</option>
                            <option value="half_day">half day</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Date : </td>
                    <td><input type="date" class="form-control" name="date[]"></td>
                </tr>

            @endforeach
            <button class="btn btn-primary" name="submit" id="submit">Submit</button>
        </table>
    </form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 2015-07-12
    • 2017-12-28
    • 2014-01-31
    相关资源
    最近更新 更多