Laravel插入的数据不适用于角度js

时间:2018-02-15 18:17:53

标签: angularjs laravel

我不知道为什么我的代码无法正常工作。 HTTP获取请求正常工作,但是当我要插入数据时,它将无法正常工作。这是我的控制器:

public function store(Request $request)
{
    $this->validate($request, [
        'name'        => 'required|max:255',
    ]);

    $item = group::create([
        'name'        => Request::input('name'),
        'description' => Request::input('description')
    ]);

    return response()->json([
        'item'    => $item,
        'message' => 'Success'
    ], 200);
}

这是我的js文件:

var app = angular.module('LaravelCRUD', [] , ['$httpProvider', function ($httpProvider) {
    $httpProvider.defaults.headers.post['X-CSRF-TOKEN'] = $('meta[name=csrf-token]').attr('content');
}]);



app.controller('itemsCtrl', ['$scope', '$http', function ($scope, $http)  {
$scope.items = [];

$scope.loaditems = function () {
    $http.get('/' + item)
    .then(function success(e) {
    $scope.items = e.data.items;
    });
};
$scope.loaditems();


$scope.item = {
    name: '',
    description: ''
};

$scope.inititem = function () {
    $scope.resetForm();
    $("#add_new_item").modal('show');
};


$scope.additem = function () {
    $http.post('/group',{
        name : $scope.item.name,
        description : $scope.item.description,
    }).then(function success(e) {
        // $scope.resetForm();
        console.log(e);
        $scope.items.push(e.data.item);
        // $scope.loaditems();
        $("#add_new_item").modal('hide');

    }, function error(error) {
        $scope.recordErrors(error);
    });
    // $scope.loaditems();
};

当我插入带有模态的东西时,没有任何成功函数触发但没有数据插入到db。

1 个答案:

答案 0 :(得分:0)

   public function store(Request $request)
   {
$this->validate($request, [
    'name'        => 'required|max:255',
]);

$item = Group::create([
    'name'        => $request->name,
    'description' => $request->description
]);

return response()->json([
    'item'    => $item,
    'message' => 'Success'
], 200);
 }

试试此代码