سؤال

I am learning AngularJS on Laravel, and I am making a simple app. So far the app displays rows from the DB in either a completed area, or a not completed area. If a user clicks the item, it moves to the opposite area.

$scope.move = function(item) {
    item.has_item = !item.has_item;
};

The above moves the item on click, however for persistence I add:

$scope.move = function(item) {
    var newState = !item.completed;
    $http.post('/items', item).success(function() {
        item.completed = newState;
    });
};

Now, I just need a route to accept this post and insert the updated value of completed to the DB.

I've tried:

Route::post('items', function()
{
    $completed = Input::get('completed');
    return $completed->push();
});

However, I have had no luck. What do I need to do to insert the value into the DB?

هل كانت مفيدة؟

المحلول

$scope.move = function(item) {
    item.has_skill = !item.has_skill;
    $http.post('/items', item).success(function(data) {
        item = data;
    });
};

//in controller
$http.get('/items').success(function(data) {
    $scope.items = data;
});

<ul>
  <li ng-repeat="item in items" ng-click="move(item)">{{item.skill_name}}</li>
</ul>

Route::post('items', function() {
    $id = Input::get('id');
    $item = Skill::find($id);
    $item->has_skill = Input::get('has_skill');
    $item->save();
    return Response::json($item);
});

Route::get('items', function() {
    return Response::json(Skill::all());
});

http://jsbin.com/bavawiko/1/edit

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top