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

rojonunoo's avatar

Saving to a has Many relation Issue

public function up() { Schema::create('students', function (Blueprint $table) {

        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->string('name');
        $table->date('dob');
        $table->string('last_school_attended');
        $table->string('avatar')->default();
        $table->decimal('student_fees');
        $table->softDeletes('deleted_at');
        $table->timestamps();
    });
}

public function up() { Schema::create('fees', function (Blueprint $table) {

        $table->increments('id');
        $table->unsignedInteger('student_id')->unsigned();
        $table->string('payment_method');
        $table->string('bank_name')->nullable();
        $table->string('account_no')->nullabe();
        $table->decimal('balance');
        $table->decimal('amount');
        $table->date('date');
        $table->timestamps();

        $table->foreign('student_id')
            ->references('id')->on('students')
            ->onDelete('cascade')
            ->onUpdate('cascade');
    });

public function store(FeesRequest $request,$id)

{
    $student = Student::FindorFail($id);

    $fees = new Fees();
    $fees->student_id= $student;
    $fees->payment_method = $request['payment_method'];
    $fees->bank_name = $request['bank_name'];
    $fees->account_no = $request['account_no'];
    $fees->amount = $request['amount'];

    $fees = Fees::create($request->all());
    return $fees;
}

Route::post('fees/store/{id}',[ 'uses'=>'FeesController@store', 'as' =>'fees.save', 'middleware'=>'auth', ]);

public function rules() { return [ // 'student_id' => 'required ', 'payment_method' => 'required ', 'balance' => 'required', 'amount' => 'required', 'date' => 'required' ]; }

Problem now is how do i get the student's id through the form

     {!! Form::open(['method'=>'POST', 'route'=>['fees.save',$student->id]]) !!}
                                {{ csrf_field() }}

{!!Form::label('amount','Amount') !!} {!! Form::number('amount', null,['class'=>'form-control' , 'step'=>'any','placeholder'=> 'Amount']) !!}

 <div class="form-group{{ $errors->has('payment_method') ? ' has-error' : '' }}">
                                    {!!Form::label('payment_method','Payment Method')  !!}
                                    {!! Form::text('payment_method', null,['class'=>'form-control'  ,'placeholder'=> 'Amount']) !!}
{!!Form::label('bank_name','Name of Bank ') !!} {!! Form::text('bank_name', null,['class'=>'form-control' , 'placeholder'=> 'Name of Bank']) !!}
                                </div>

                                <div class="form-group{{ $errors->has('account_no') ? ' has-error' : '' }}">
                                    {!!Form::label('account_no','Account Number')  !!}
                                    {!! Form::text('account_no', null,['class'=>'form-control' ,'placeholder'=> 'Account Number']) !!}

                                   

                                <div class="form-group{{ $errors->has('date') ? ' has-error' : '' }}">
                                    {!!Form::label('date','Date of Payment')  !!}
                                    {!! Form::date('date', null,['class'=>'form-control']) !!}

                                   
PAY {!! Form::close() !!}

i can't seem to get the student_id through the form

0 likes
4 replies
tisuchi's avatar

Sorry.... Can able to ready the code properly. Can you update your question to make it more readable?

tisuchi's avatar

Just check your question where many code are very hard to read. Try to make it proper format that can make it readable...

Please or to participate in this conversation.