Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Fajar's avatar
Level 2

Multiple input data

I want to store the student_id and nominal data as an array

can you guys help me

create.blade.php

@foreach ($students as $student)
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <input type="text" value="{{$student->name}}" id="" class="form-control">
                                        <input type="hidden" name="student_id[]" value="{{$student->id}}" id="" class="form-control">
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <input type="text" name="nominal[]" id="" class="form-control">
                                    </div>
                                </div>
                            </div>
                        @endforeach
0 likes
9 replies
DhPandya's avatar

@fajar The HTML code you currently have is already in the [] except the student name field. Could you please elaborate your requirements ?

Fajar's avatar
Level 2

@DhPandya my need is

I want to save savings data

program flow I click on the name of the month then I only need to fill in the nominal value for each name then I send it simultaneously

tangtang's avatar

@fajar

you can follow this steps as reference

in your blade

<form method="post" action="{{ route('postData') }}"> // assume you allready define the route
	@csrf
	// your other input field here
	@foreach ($students as $student)
		<input type="text" value="{{$student->name}}" id="" class="form-control">
		<input type="hidden" name="student_id[]" value="{{$student->id}}" id="" class="form-control">
		<input type="text" name="nominal[]" id="" class="form-control">
	@endforeach
	<button type="submit">Submit</button>
</form>

in controller

public function postData(Request $request)
{
	// your other validation here

    $studentIds = $request->input('student_id');
    $nominals = $request->input('nominal');

    // loop through the submitted data and update the records
	foreach ($studentIds as $key => $studentId) {
        $student = new YourTargetModel(); // replace model with your real model
        $student->student_id = $studentId;
        $student->nominal = $nominals[$key];
        $student->save();
    }

    return redirect()->route('yourRoute')->with('success', 'Success');
}
Fajar's avatar
Level 2

@tangtang

Do the data types in the schema need to be changed?

public function up()
    {
        Schema::create('savings', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('month_id');
            $table->unsignedInteger('student_id');
            $table->string('nominal');
            $table->timestamps();
        });
    }
Fajar's avatar
Level 2

@tangtang

month_id is gone in here

public function postData(Request $request)
{
	// your other validation here

    $studentIds = $request->input('student_id');
    $nominals = $request->input('nominal');

    // loop through the submitted data and update the records
	foreach ($studentIds as $key => $studentId) {
        $student = new YourTargetModel(); // replace model with your real model
        $student->student_id = $studentId;
        $student->nominal = $nominals[$key];
        $student->save();
    }

    return redirect()->route('yourRoute')->with('success', 'Success');
}
tangtang's avatar

@Fajar

just add and customize the code as you need. if you have field name month_id in database make sure you have add the field in your form and in your controller add the

	$monthId= $request->input('month_id');
	$studentIds = $request->input('student_id');
    $nominals = $request->input('nominal');

and add the $monthId in the looping task

foreach ($studentIds as $key => $studentId) {
        $student = new YourTargetModel(); // replace model with your real model
   		$student->monthId = $monthId;
     	$student->student_id = $studentId;
        $student->nominal = $nominals[$key];
        $student->save();
    }
tangtang's avatar
tangtang
Best Answer
Level 6

@Fajar

make sure all field names match the column names in the database.

don't copy paste all the response code. adjust it to your application needs. 😁

for example in my response I type like this $student->monthId = $monthId;, dont just copy paste it.

analyze first and adjust it to your field name. like $student->month_id = $monthId;

not all response can be copy paste. you need to analyze and understanding the flow and process before use it to your app.

but yes, keep the good work. you will get after practice it many times. 😁

Please or to participate in this conversation.