Home > Software engineering >  Unable to save data in database laravel
Unable to save data in database laravel

Time:08-27

I have a problem , saving data to the database from the input form that I made. The code is running well but the form is not saving the data I input from the form to the database. Did I miss something? when I click on the button to save the info to the database, nothing seems to happen(meaning no error).

Here is my jquery:

var percentageSupport = "";

$('#btnSaveFinanicialSupport').on('click', function () {

    if (patientId == "") {
        alert("kindly Enter Patient Id  ")
        return false;
    }

    if ($('#currentSupport').val() == 0) {
        alert('Kindly Give Financial Support');
        return false;
    }

    var totalPayableAmount = $('#payableAmount').val();
    var currentSupportAmount = $('#currentSupport').val();


    if (currentSupportAmount > totalPayableAmount) {
        alert('Kindly Enter Correct Support Amount');
        return false;
    }
    //here to start to set data to call save service.

    var percantageSupportAmount = Math.round((currentSupportAmount / totalPayableAmount) * 100);


    var supportType = $('select.supportType').val();
    var token = supportType.split("@");
    var supportTypeId = token[0];
    var supportType = token[1];
    if (supportType == "SELECT") {
        alert('kindly Select Support Type');
        return false;
    }
    var comments = $('#comments').val();
    var orderIds = $("#tbody input:checkbox:checked").map(function () {
        return $(this).attr("orderId");
    }).get();

    var serviceIds = $("#tbody input:checkbox:checked").map(function () {
        return $(this).attr("serviceId");
    }).get();

    var serviceCost = $("#tbody input:checkbox:checked").map(function () {
        return $(this).attr("price");
    }).get();

    var servicePayableAmount = $("#tbody input:checkbox:checked").map(function () {
        return $(this).attr("payableAmount");
    }).get();

    var serviceFinancialSupport = $("#tbody input:checkbox:checked").map(function () {
        return $(this).attr("financialSupport");
    }).get();

    $.ajax({
        type: 'post',
        data: {
            patientId: patientId, supportTypeId: supportTypeId, percantageSupportAmount: percantageSupportAmount,
            comments: comments, serviceIds: serviceIds, orderIds: orderIds, servicePayableAmount: servicePayableAmount,
            serviceFinancialSupport: serviceFinancialSupport, serviceCost: serviceCost
        },
      
        url: '/saveFinancialSupport',
        success: function (data) {
            alert("Financial Support Given Successfully")
            $("#tbodyy").html("");
            $("#tbody").html("");
            $('#totalAmount').val('');
            $('#totalAmount').text('');
            $('#paidAmount').val('');
            $('#paidAmount').text('');
            $('#supportamount').val('');
            $('#supportamount').text('');
            $('#comments').val('');
            $('#comments').text('');
            $('#totalamount').val('');
            $('#totalamount').text('');
            $('#totalpayable').val('');
            $('#totalpayable').text('');
        }
    });
})

Here is my controller:

 //save financial support
     public function saveFinancialSupport(Request $req){
        $output = array();
        $orderIds = $req->orderids;
        $serviceIds = $req->serviceids;
        $servicePayableAmount = $req->servicePayableAmount;
        $serviceFinancialSupport = $req->serviceids;
        $serviceCost = $req->serviceCost;
        $obj = new SessionClass();
        $array = array();
        for ($i=0; $i < count($orderIds) ; $i  ) {
                    $price = $serviceCost[$i];
                    $support = ($servicePayableAmount[$i] * $req->percentageSupport)/100;
                    $array[$i] = array(
                    "financialSupport" => $support,
                    "patientId" => $req->patientId,
                    "orderId" => $orderIds[$i],
                    "supportTypeId" => $req->supportTypeId,
                    "price" => $serviceCost[$i],
                    "sign" => " ",
                    "percentageSupport" => $req->percentageSupport,
                    "comments" => $req->comments,
                    "active" => "Y",
                    "refFormNo" => "0",
                    "serviceId" => $serviceids[$i],
                    "id" => "123",
                    "locationId" => $obj->getLocationId(),
                    "orgId" => $obj->getOrgId(),
                    "sessionId" => $obj->getSessionId(),
                    "crtdBy" => $obj->getUserId(),
                    "crtdTerminalId" => "123");
         }

         $host = new HostClass();
         $obj = new SessionClass();
        $data = json_encode($array);
        $response = Http::post('127.0.0.1:9000/saveFinancialSupport', $array);
        return redirect()->back()->with('alert', 'Record Save Successfully');

    }

Here is my web route:

Route::post('saveFinancialSupport' , 'SupportController@saveFinancialSupport');

Here is my web route:

@extends('layouts.theme')
@section('content')
<meta name="csrf-token" content="{{ csrf_token() }}">
    <div >
        @include('patientPanel.patientPanel')

        <form method="post" action="saveFinancialSupport" id="fsform">
            @csrf
            <div >
                <div >
                    <div >
                        <div >
                            <label>From Date</label>
                        </div>
                        <div >
                            <input type="Date" name="fromDate" id="fromDate" 
                                placeholder="Client">
                        </div>
                        <div >
                            <label>To Date</label>
                        </div>
                        <div >
                            <input type="Date" name="toDate" id="toDate" >
                        </div>
                        <div  style="font-size: 14px; font-weight:bold;">
                            <label><input type="checkbox" name="All Date" id="allDates">&nbsp All Dates</label>
                        </div>
                        <div >
                            <label>Support Limit</label>
                        </div>
                        <div >
                            <input type="text" name="supportLimit" id="supportLimit" 
                                value="" readonly="">
                        </div>
                        <div >
                            <label>Support Given</label>
                        </div>
                        <div >
                            <input type="text" name="supportgiven" id="supportgiven" 
                                value="" readonly="">
                        </div>
                    </div>
                </div>
            </div>
            <!-- //end card one -->


            <div >
                <div >
                    <div >
                        <div >
                            <div style="height:200px;overflow-y: auto;" >
                                <table id="tblServiceOrders" >
                                    <thead >
                                        <tr  style="color: white;">
                                            <th>Order Date</th>
                                            <th>Order By</th>
                                            <th>Service Name</th>
                                            <th>Cost</th>
                                            <th>Support</th>
                                            <th>Payable</th>
                                            <th>Select</th>
                                        </tr>
                                    </thead>
                                    <tbody id="tbody" >
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div >
                <div  id="tblSupportDetailCollapse">
                    <div >
                        <div >
                            <div style="height:100px;overflow-y: auto;" >
                                <table id="supportDetail" >
                                    <thead >
                                        <tr  style="color: white;">
                                            <th>Order Date</th>
                                            <th>Order By</th>
                                            <th>Service Name</th>
                                            <th>Cost</th>
                                            <th>Support</th>
                                            <th>Payable</th>
                                            <th>Select</th>
                                        </tr>
                                    </thead>
                                    <tbody id="tbody" >
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- end second table -->
            <!-- here is start final calculation of financial support -->

            <div >
                <div >
                    <div >
                        <div >
                            <label>Total Amount</label>
                        </div>
                        <div >
                            <input type="text" name="totalAmount" id="totalAmount" 
                                value="" readonly="">
                        </div>
                        <div >
                            <label>Support </label>
                        </div>
                        <div >
                            <input type="text" name="supportAmount" id="supportAmount"
                                 value="" readonly="">
                        </div>
                        <div >
                            <label>Payable</label>
                        </div>
                        <div >
                            <input type="text" name="payableAmount" id="payableAmount"
                                 value="" readonly="">
                        </div>
                        <div >
                            <select  id="supportType">
                                <option value="SELECT">SELECT</option>
                            </select>
                        </div>
                        <div >
                            <select  id="percentageSupport">
                                <option value="SELECT">SELECT</option>
                            </select>
                        </div>
                        <div >
                            <input type="text" name="currentSupport" id="currentSupport"
                                 value="" >
                        </div>
                        <div >
                            <label>Net Payable</label>
                        </div>
                        <div >
                            <input type="text" name="netPayable" id="netPayable" 
                                value="" readonly="">
                        </div>
                    </div>
                </div>
            </div>
            <!-- here calacualtion here  -->

            <div >
                <div >
                    <div >
                        <div >
                            <label>Remarks</label>
                        </div>
                        <div >
                            <input type="text" name="comments" id="comments" >
                        </div>
                    </div> <!-- row end -->
                </div> <!-- card body forms -->
            </div> <!-- card mb-2 -->

            <div >
                <div >
                    <div >
                        <div >
                        </div>
                        <div >
                            <a  id="btnSaveFinanicialSupport" style="color:white">Financial Support</a>
                        </div>
                        <div >
                            <a  id="">Cancel Support</a>
                        </div>
                        <div >
                            <a  id="btngenerateinvoice"
                                style="color:white">Patient Support History</a>
                        </div>
                        <div >
                            <button  id="supportDetail" type="button" data-toggle="collapse"
                                data-target="#tblSupportDetailCollapse" aria-expanded="false"
                                aria-controls="tblSupportDetailCollapse">
                                Support Detail
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <link rel="stylesheet" href="{{ url('PublicCssForm/PacslinkCustomcss.min.css') }}">
    <script src="{{ url('js/jquery.min.js') }}"></script>
    <script src="{{ url('js/moment.min.js') }}"></script>
    <script src="{{ url('theme/bootstrap.min.js') }}"></script>
    <script src="{{ url('OrderInvoiceJs/FinancialSupport.min.js') }}"></script>
@endsection

CodePudding user response:

As @aynber mentions in his comment to your question, you are not creating or saving a model, which is obviously the main problem here.

I see that you have this line: $obj = new SessionClass(); which I assume is the model you want to save, but you do nothing with it, and you also overwrite it below your loop.

First of all, you need to have fields fillable on that model and then either save() or create() with data.

You can see an example of it here https://laravel.com/docs/9.x/eloquent#inserts

Simple example:

class Object extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['data1', 'data2', 'data3'];
}

You can then use this class to do something like:

$obj = new Object;
$object->save([
   'data1' => 'some value',
   'data2' => 'some value',
   'data3' => 'some value',
])

// You can also do it like, which will create and save it for you.
$obj = Object::create([
   'data1' => 'some value',
   'data2' => 'some value',
   'data3' => 'some value',
]);

If the fields are not described in the $fillable array of the model, they will not be persisted to the database.

  • Related