vendredi 2 avril 2021

How To submit form by one submit button in foreach loop in laravel?

This is my create page. Here there is form in foreach loop having on submit button. (This is my create page. Here there is form in foreach loop having on submit button.)

<form method="post" action="">
        @csrf
        <table>
            @foreach ($students as $student)

                <tr>
                    <td>Name : </td>
                    <td><input type="text" class="form-control" name="name" value="" 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>

This is my TypeController. This is my TypeController. I want to store data in foreach loop. (This is my TypeController. This is my TypeController. I want to store data in foreach loop)

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');
}

This is my Type model which has relation with Student table (This is my Type model which has relation with Student table)

use HasFactory;

protected $table = 'types';

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

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


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire