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

unknownUser17's avatar

Internal Server Error in Saving Records to database through API

I have 2 tables related to creating credit notes which allows selecting multiple invoices. I was able to store the data into a database using postman(api controller), but having problems in API integration with the frontend.

It returns "Internal Server Error" when I submit the form...

Database

Credit Note

id   credit_note_group_id  invoice_id   code     remarks      amount
7    4                     5            CN0006   cn remarks   150.00
8    4                     8            CN0007   cn remarks   50.00

Credit Note Group

id   content_id    content_type        code      type            remarks          amount
4    3             App\Models\Member   CNG0003   Cancellation    admin remarks    200.00

credit_note_group_id in Credit Note table is the primary key in Credit Note Group table and it's auto-increment. If two invoices are selected, two rows will be created in Credit Note table, while Credit Note Group table will store the related member data and the total amount of credit note.

How should I correct the codes in the controller so that the requested data can store in credit_note_group table as well through column credit_note_group_id in credit_note table? Many thanks to anyone who can help!

Controller(UI)

public function store(Request $request)
{
    $request->merge([
        'content_id'       => $request->content_id,
        'content_type'  => 'App\Models\Member',       
        'balance'            => $request->amount,
        'status'                => 'Open',
        'created_by'     => Session::get('user')['id'],
    ]);

    $request_data = $request->except([
        'type' => $request->type,
    ]);

    $response = HttpClient::request('post', $this->api_module, $request_data);
    if ($response->getStatusCode() == 201) {
        flash('The new credit note has been created')->success()->important();
        return redirect()->route('credit-notes.index');
    } else {
        flash('The new credit note could not be created')->error()->important();
        return back()->withInput();
    }
}

create.blade.php

@php
    $id = (Request::query('id')) ? Request::query('id') : null;
@endphp

//----- Credit Note Group ID: store in "credit note" table
{!! Form::hidden('credit_note_group_id', $id) !!}

<div class="col-sm-4 form-group controls">
    //---- Member ID: store in "credit note group" table
    {!! Form::label('content_id', 'Membership No') !!}     // how to point this to "content_id" (member id) in credit note group table?
    {!!
        Form::select('credit_note_group_id', $member, $id, [
            'class' => 'form-control select2 required',
            'placeholder' => 'Type to Search',
            'required'
        ])
    !!}
</div>

///

<div class="row">
    <div class="col-sm-8 form-group">
        {!! Form::label('invoice_id', 'Invoice') !!}
        {!! Form::select('invoice_id[]', $invoices['list'], null, [  'class' => 'form-control select2 required', 'multiple' => 'multiple', 'required']) !!}
    </div>
</div>

CreditNote.php

public function creditNoteGroup()
{
    return $this->belongsTo('App\Models\CreditNoteGroup');
}

CreditNoteGroup.php

public function creditNotes()
{
    return $this->hasMany('App\Models\CreditNote');
}

public function content()
{
    return $this->morphTo();
}
0 likes
0 replies

Please or to participate in this conversation.