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>
<tr>
<th><p style="margin: 20px">Name : </p></th>
<th><p style="margin: 20px">Type : </p></th>
<th><p style="margin: 20px">Date : </p></th>
</tr>
@foreach ($students as $student)
<tr>
<th><p style="margin: 20px"></p></th>
<input name="name[]" type="hidden" value="">
<th><p style="margin: 20px">
<select name="type[]" id="type">
<option value="present">present</option>
<option value="absent">absent</option>
<option value="half_day">half day</option>
</select></p>
</th>
<th><p style="margin: 20px"><input type="date" class="form-control" name="date[]"></p></th>
</tr>
@endforeach
<td><button class="btn btn-primary" name="submit" id="submit">Submit</button></td>
</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 index()
{
// $types = Type::all();
$types = Type::orderBy('date', 'desc')->paginate(50);
$students = Student::all();
return view('types.index', compact('types', 'students'));
}
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 = 'students';
protected $fillable = [
'name',
];
public function type()
{
return $this->hasMany(Type::class);
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire