时间:2019-06-02 01:41:59

标签: jquery laravel

Soy nuevo trabajando con laravel y hay muchas cosas que se me escapan,el issuea es que hace varios dias en esto tratando de subir una imagen con Formulaand Usando Ajax y los otros campos trabajan bien al crear y hacer un update Per image image no logro subirla es como si simplemente no estubiera 在配方奶粉中添加营养素后会变质,在问题上需要解决的问题

埃斯塔埃斯拉移民#p>

    Schema::create('tours', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('description');
        $table->string('photo')->nullable();
        $table->timestamps();
    });

以模态形式使用我们的广告

    {!! Form::model($model, [

    'route' => $model->exists ? ['tours.update',app()->getlocale(), $model->id] : ['tours.store', app()->getlocale()],
    'method' => $model->exists ? 'PUT' : 'POST',
    'enctype' => 'multipart/form-data']) !!}    

    <div class="form-group">
        <label for="" class="control-label">Name</label>
        {!! Form::text('name', null, ['class' => 'form-control', 'id' => 'name']) !!}
    </div>

    <div class="form-group">
        <label for="" class="control-label">Description</label>
        {!! Form::text('description', null ,['class' => 'form-control', 'id' => 'description']) !!}
    </div>

    <div class="form-group">
        <label for="" class="control-label">Foto</label>
        {!! Form::file('photo', null ,['class' => 'form-control', 'id' => 'photo']) !!}
    </div>
{!! Form::close() !!}

Este es el metodo商店Dentro del Controlador

    public function store($locale, Request $request)
    {
         $this->validate($request,[
        'name' => 'required|max:120',
        'description' => 'required|max:120'
        'photo' => 'image|max:2048'
        ]); 
         $input = $request->all();
        $input ['photo'] = null;

        if ($request->hasFile('photo'))
        {
            $input ['photo'] = '/image'.str_slug($input['name'],'-').'.'.$request->photo->getClientOriginalExtension();
            $request->photo->move(public_path('/image'), $input['photo']);            
        }

        Tour::create($input);
        return response()->json(['success' => true]);
}

是雅各布·阿贾克斯:

    //SAVE / UPDATE
$('#modal-btn-save').click(function(event){
    event.preventDefault();

    var form = $('#modal-body form'),
    url = form.attr('action'),
    method = $('input[name=_method]').val() == undefined ? 'POST' : 'PUT';

    form.find('.help-block').remove();
    form.find('.form-group').removeClass('has-error');

    $.ajax({
        url : url,
        method : method,
        data : new FormData($('#modal form')[0]),
        contentType : false,
        proceessData : false,
        success: function (response){
            form.trigger('reset');
            $('#modal').modal('hide');
            $('#datatable').DataTable().ajax.reload();

            swal({
                type : 'success',
                title : 'Success!',
                text : 'Data has been saved!'
            });
        },
        error : function (xhr){
            var res = xhr.responseJSON;
            if ($.isEmptyObject(res) == false){
                $.each(res.errors, function (key, value){
                    $('#' + key)
                        .closest('.form-group')
                        .addClass('has-error')
                        .append('<span class="help-block"><strong>' + value + '</strong></span>')
                });
            }
        }
    })
});

muchas gracias por adelantado

0 个答案:

没有答案