在从控制器到模型的数据插入过程中,Laravel REST API会产生500内部服务器错误

时间:2016-10-29 05:51:58

标签: rest laravel-5.2

我创建了一个REST API,用于从Modal插入数据。提交表单后,我从我的"商店"控制器。但是当我调用Model函数将数据插入Database时,我在控制台上收到500内部服务器错误。

我尝试了两种方法1.使用Eloquent ORM                            2.使用查询生成器

我的控制器:

  public function store(Request $request)
    {
        $category_name        = $request->category_name;
        $category_entry_date  = $request->category_entry_date;
        $category_info = array(
                'Supplier_Name'        => $category_name,
                'Supplier_Des'         => $category_entry_date,
                'Shop_Id'              => 1
                );
            // Insert data into database      
            Product::CreateProductCategory($category_info);
        return response()->json(['success_massege'=>'Category Added Successfully']);  
    }

JS:

$("#category_submit").click(function (e) {
    event.preventDefault();
    var category_name = $('#category_name_id').val();
    var category_entry_date = $('#category_entry_date_id').val();

    if (category_name && category_entry_date) {

        $.post("/api/api/product_category", {category_name: category_name, category_entry_date: category_entry_date}).done(function (data) {
            $('#category_name_id').val("");
            $('#category_entry_date_id').val("");

            var success_massege_dialogbox = '';
            success_massege_dialogbox += '<div class="alert alert-success fade in">';
            success_massege_dialogbox += '<a href="#" class="close" data-dismiss="alert">&times;</a>';
            success_massege_dialogbox += '<strong>Success!</strong>' + data.success_massege + '</div>';
            $('#success_massege').append(success_massege_dialogbox);

        });
    }
    else {
        alert('Please give a category name and entry date');
    }

});

型号:

static function CreateProductCategory($category_info){
        DB::table('product_category_info')->insert($category_info);
    }

查看:

<div class="modal fade" id="addCategory" tabindex="-1" role="dialog" aria-labelledby="addCategoryLabel">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title" id="addCategoryLabel">Add Category</h4>
                        </div>

                        <form action="{{ route('product_category.store') }}" method="POST">
                        <div class="modal-body">
                            <div class="modal-body">
                                <div class="box-body">
                                    <div class="row form-group">
                                        <div class="col-md-3 form-level"></div>
                                        <div class="col-md-9" id="success_massege"> </div>
                                    </div>

                                    <div class="row form-group">
                                        <div class="col-md-3 form-level"><label>Category Name<b class="mandetory_star">*</b></label></div>
                                        <div class="col-md-9" id="email-error-dialog"> 
                                            {{Form::text('category_name','', $attributes = array('class' => 'form-control',
                                                                                                 'id' => 'category_name_id',
                                                                                                 'data-validation'=>'alphanumeric', 'data-validation-allowing'=>'-_',
                                                                                                 'data-validation-error-msg'=>'Please Enter a Valid Category Name',
                                                                                                 'data-validation-error-msg-container'=>'#email-error-dialog',
                                                                                             ))}}
                                        </div>
                                    </div>
                                    <div class="row form-group">
                                        <div class="col-md-3 form-level"><label>Entry Date<b class="mandetory_star">*</b></label></div>
                                        <div class="col-md-9" id="date-error-dialog"> 
                                            {{Form::date('entry_date','', $attributes = array('class' => 'form-control',
                                                                                              'id' => 'category_entry_date_id',
                                                                                              'data-validation'=>'date',
                                                                                              'data-validation-error-msg'=>'Please Enter a Valid Date',
                                                                                              'data-validation-error-msg-container'=>'#date-error-dialog'))}}
                                        </div>
                                    </div>
                                </div><!-- /.box-body -->
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                            <button id="category_submit" type="submit" class="btn btn-primary save-category"> Save category </button>
                        </div>

                        </form>
                    </div>
                </div>
            </div>

0 个答案:

没有答案