在laravel

时间:2016-02-04 08:09:37

标签: php mysql arrays json laravel

在laravel中,我正在尝试更新几个表和行。我有2个不同用户收到和检查的项目。

在我的show.blade.php中,我有这个验证按钮和提交按钮,具体取决于用户。

@if (($current_user_id  != $saved_receiver_id) && ($saved_receiver_id != $not_yet_received))    

    <div class="row padder m-b">
        <div class="col-md-12">
            <label for="received_by" class="pull-left" >Received by:</label> <span class="fa"></span>
            <input type="hidden" name="receiver_id" value="{{ $saved_receiver_id }}" >{{ $receiver_username }}</input>
        </div>
    </div>
    <div class="row padder m-b">
        <div class="col-md-12">
            <label for="received_date" class="pull-left" >Received date:</label> <span class="fa"></span>
            <input type="hidden" name="received_date" value="{{ $received_date }}" >{{ $received_date }}</input>
        </div>
    </div>
    <input type="hidden" name="purchase_orders_id" value="{{ $purchase_order_number }}">
    <input type="hidden" name="checker_id" value="{{ $current_user_id }}">
    <div class="row padder m-b">
        <div class="col-md-12">
            <label for="final_checker_remarks" class="pull-left" >Final Remarks</label>
            <textarea class="form-control col-md-12" name="final_checker_remarks" value="{{ $final_checker_remarks }}" id="final_checker_remarks">{{ $final_checker_remarks }}</textarea>
        </div>
    </div>
    <br />
    <div class="row">
        <div class="col-md-12">
            <button type="button" class="pull-right btn btn-success btn-sm submit-btn" id="update-form-submit" data-action="verified">Verified</button>
        </div>
    </div>
    @else
    <div class="pull-right">
        <div class="btn-group">
            <button type="button" class="btn btn-info btn-sm submit-btn" id="update-form-submit" data-action="submit">Submit List</button>
        </a>
    </div>
@endif

现在在我的ReceivesController.php中,我有这个postUpdate函数,

public function postUpdate(Request $request)
{
    if (! $request->ajax()) {
        abort(404);
    }

    $items = json_decode($request->items);
    $action = json_decode($request->action);        
    if(!count($items) && !count($items->purchase_item_id)){
        return false;
    }

    $cnt = count($items->purchase_item_id);     
// Receiver Submit function 
    if ($action == "submit") {

// Saves the received id of the one who received to purchase order table        
        DB::table('purchase_orders')
        ->where('id', $items->purchase_orders_id)
        ->update([
            'receiver_id'       =>  $items->receiver_id,

        ]);


// Saves the quantity received and receiver remarks to purchase items table 
        for($i=0; $i<$cnt; $i++){
            DB::table('purchase_items')
            ->where('id', $items->purchase_item_id[$i])
            ->update([
                    'quantity_received' =>  $items->quantity_received[$i], 
                    'receiver_remarks'  =>  $items->receiver_remarks[$i],
                ]);
        }   
// Items Received Success Message           
        $message = 'Items Received';
    } 
// QA or Checker Finalize function  
    else {

// Saves the checker id, and final checker remarks of the one who made the QA to purchase order table   
        DB::table('purchase_orders')
        ->where('id', $items->purchase_orders_id)
        ->update([
            'checker_id'            =>  $items->checker_id,
            'final_checker_remarks' =>  $items->final_checker_remarks,
        ]);
// Saves the quality received and checker remarks to purchase items table   
for($i=0; $i<$cnt; $i++){                       

            $quality_received = $items->quality_received;
            if(is_array($items->quality_received)){
                $quality_received = $items->quality_received[$i];
            }

            $checker_remarks = $items->checker_remarks;
            if(is_array($items->checker_remarks)){
                $checker_remarks = $items->checker_remarks[$i];
            }

            $quantity_received = $items->quantity_received;
            if(is_array($items->quantity_received)){
                $quantity_received = $items->quantity_received[$i];
            }

            $receiver_remarks = $items->receiver_remarks;
            if(is_array($items->receiver_remarks)){
                $receiver_remarks = $items->receiver_remarks[$i];
            }


            DB::table('purchase_items')
            ->where('id', $items->purchase_item_id[$i])
            ->update([
                    'quality_received'  =>  $quality_received, 
                    'checker_remarks'   =>  $checker_remarks,                       
                    'quantity_received' =>  $quantity_received, 
                    'receiver_remarks'  =>  $receiver_remarks,
                ]);
// Increments or Adds the quantity received to items table  
            DB::table('items')
            ->where('purchase_items.id', $items->purchase_item_id[$i])
            ->join('purchase_items', 'items.id', '=', 'purchase_items.item_id')
            ->increment('items.measurement', $items->quantity_received[$i]);

        }
/   / Items Finalized Success Message       
        $message = 'Items Verified';
    }
// Returns Success Message 
    return response()->json([
            'success' => true,
            'message' => $message
        ]);
}

现在我的问题只是输入区域中输入的单词的第一个字母正在保存,而在其他字母中它们没有保存,但在其他情况下,它可以保存。我知道它很奇怪,但我无法找到我的代码的哪一部分正在做这样的结果,我需要做些什么来正确更新我的表?在此先感谢您的帮助。

更新:这是我的receive-form-function.js

    /* ========================================================================
 *  Initialize Pages
 * ======================================================================== */
    $(initialPages);

/* ========================================================================
 *  Major function
 * ======================================================================== */

/* ==== function to init this page === */
function initialPages($) {

    // if($('#receives-list-table').length){
    //  DataTables("#receives-list-table", "receives");
    // }

    if($('#receiveItems-list-table').length){
            $("#receiveItems-list-table").DataTable({
            responsive: true,
            ordering: false,
        });
    }

    $('#update-form-submit').on('click', function(){
        var action = $(this).data('action');
        updateReceiveItem(action);
    });
    clearInputs();
}

/* === dataTables === */
function DataTables(selector, controller) {
    $(selector).DataTable({
        responsive: true,
        processing: true,
        serverSide: true,
        ajax: url+'/'+controller+'/paginate'
    });
}

function updateReceiveItem(action){
    loadingModal('show','Updating ....');
    ajaxCsrfToken();

    var data = $('#receiveItems_id').serializeArray();
        data = JSON.stringify(data);
        // data = JSON.parse(data);
        data = JSON.stringify($('#receiveItems_id').serializeObject());
        // data = $('#receiveItems_id').serializeObject();

    $.ajax({
            url: url+'/receives/update',
            type: 'post',
            data: {'items':data, 'action': action},
            dataType: 'json',
            complete: function() {      
                loadingModal('close');
            },
            error: function(result) {

            },
            success: function(result) {

                successAlert('#receiveItems-result', result.message);
            // if (result.success) {

            //      $('input, select, textarea').attr('disabled', true);

            //  } else {
            //      alert(result.message);
            //  }
            }

        });
        console.log(data);
        return false;
}
/**
 * Use to format serialize data and convert to json data
 *
 * Usage: JSON.stringify($('form').serializeObject())
 */
$.fn.serializeObject = function() {
    var o = Object.create(null),
        elementMapper = function(element) {
            element.name = $.camelCase(element.name);
            return element;
        },
        appendToResult = function(i, element) {
            var node = o[element.name];

            if ('undefined' != typeof node && node !== null) {
                o[element.name] = node.push ? node.push(element.value) : [node, element.value];
            } else {
                o[element.name] = element.value;
            }
        };

    $.each($.map(this.serializeArray(), elementMapper), appendToResult);
    console.log(o);
    return o;
};

1 个答案:

答案 0 :(得分:1)

我的$ .fn.serializeObject = function()上面似乎有错误,我尝试使用另一个$ .fn.serializeObject = function(),它给了我想要的json对象。这是我现在使用的$ .fn.serializeObject = function()。

std::advance