Laravel 5.2 ajax更新问题

时间:2016-09-09 10:00:11

标签: php ajax laravel laravel-5.2

我根本无法让这个工作。

我在这里做错了什么?谢谢! :-)

验证器返回字段是必需的,我似乎无法访问任何数据。

控制器:

public function updateGlobalData(Request $request){

    $validator = Validator::make($request->all(), [
        'company_name' => 'required',
        'adress' => 'required',
        'city' => 'required',
        'zip' => 'required',
        'country' => 'required',
        'cvr' => 'required',
        'email' => 'required|email',
        'phone' => 'required'
    ]);

    $errors = $validator->errors();

    if ($validator->fails()) {
        return response()->json([
            'success' => false,
            'message' => $errors
        ], 422);
    }

    $globalData = GlobalData::find($request->id)->update($request->all());

    $response = [
        'status' => 'success',
        'msg' => 'Hotel created successfully'
    ];

    // Session::flash('success', trans('Global data was updated'));

    return \Response::json( $response );

}

路线:

Route::post('admin/site-settings/global-data/update', ['as' => 'admin/global-data-update', 'uses' => 'admin\GlobalDatasController@updateGlobalData']);

型号:

class GlobalData extends Model
{
   protected $fillable = ['company_name', 'address', 'city', 'zip', 'country', 'cvr', 'email', 'phone', 'logo' ];
 }

的Ajax:

    function update () {
        var url = '{{route('admin/global-data-update')}}';
        var part = '{{ $globalData->part }}';
        var data = $("#update_form").serialize();
        var jqxhr = $.post(url,  {
            data: data,
            part: part
            },
            function() {
        })
        .done(function() {
            var json_response = jqxhr.responseText;
            if(json_response) {
                load_data('{{route('admin/global-data-edit')}}', '{{ $globalData->part }}');
            } else {
                console.log("Error");
            }
        })
    }

我的表格:

{!! Form::model($globalData, ['id' => 'update_form']) !!}

<div class="form-group">
    {!! Form::text('company_name', null, array('class' => 'form-control', 'placeholder' => trans('Company name'))) !!}
</div>

<div class="form-group">
    {!! Form::text('address', null, array('class' => 'form-control', 'placeholder' => trans('Address'))) !!}
</div>

<div class="form-group">
    {!! Form::text('city', null, array('class' => 'form-control', 'placeholder' => trans('city'))) !!}
</div>

<div class="form-group">
    {!! Form::text('zip', null, array('class' => 'form-control', 'placeholder' => trans('zip'))) !!}
</div>

<div class="form-group">
    {!! Form::text('country', null, array('class' => 'form-control', 'placeholder' => trans('country'))) !!}
</div>

<div class="form-group">
    {!! Form::text('cvr', null, array('class' => 'form-control', 'placeholder' => trans('cvr'))) !!}
</div>

<div class="form-group">
    {!! Form::text('email', null, array('class' => 'form-control', 'placeholder' => trans('email'))) !!}
</div>

<div class="form-group">
    {!! Form::text('phone', null, array('class' => 'form-control', 'placeholder' => trans('phone'))) !!}
</div>

<div class="form-group">
    {{ Form::hidden('id') }}
</div>


{!! Form::button(trans('Save changes'), ['class' => 'pull-right btn btn-success save', 'id' => 'save' ]) !!}

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

我真的很想得到一些帮助,谢谢你的时间! : - )

1 个答案:

答案 0 :(得分:0)

地址字错误

$validator = Validator::make($request->all(), [
    'company_name' => 'required',
    'adress' => 'required',
    'city' => 'required',
    'zip' => 'required',
    'country' => 'required',
    'cvr' => 'required',
    'email' => 'required|email',
    'phone' => 'required'
]);