jeudi 2 mars 2017

I cant insert data to db using ajax in laravel 5.2

I wonder if its because of the model, and I dont know where to put model file and what to write on it, im new to laravel 5.2

html (testing.blade.php)

<input type="text"id="name" name="name">
<button type="submit" id="add">ADD</button>

ajax

    $("#add").click(function() {
        $.ajax({
            type: 'post',
            url: 'addItem',
            data: {
                '_token': $('input[name=_token]').val(),
                'name': $('input[name=name]').val()
            },
            success: function(data) {
                alert(data);
            },
        });
        $('#name').val('');
    });

routes

Route::post ('test', 'CommentsController@addItem' );

controller

public function addItem(Request $request) {
    $data = new Comments ();
    $data->comment = $request->name;
    $data->save ();
    return response ()->json ( $data );
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire