【问题标题】:Post Updated Value Using Eloquent使用 Eloquent 发布更新值
【发布时间】:2014-05-31 22:04:54
【问题描述】:

我正在 Laravel 上学习 AngularJS,我正在制作一个简单的应用程序。到目前为止,该应用程序在已完成区域或未完成区域中显示数据库中的行。如果用户单击该项目,它会移动到对面的区域。

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

以上内容会在点击时移动项目,但是为了持久性,我添加了:

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

现在,我只需要一条路由来接受这篇文章并将完成的更新值插入数据库。

我试过了:

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

但是,我没有运气。我需要做什么才能将值插入数据库?

【问题讨论】:

  • $completednot 是一个对象,它将是一个字符串(如果不存在,则为 null)。当您没有使用 Laravel 进行任何适当的数据库调用时,很难回答您。

标签: php ajax angularjs laravel eloquent


【解决方案1】:
$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

【讨论】:

  • 当我点击一个项目时,我仍然得到一个500 (Internal Server Error)。您还需要查看其他代码来提供帮助吗?感谢您抽出宝贵时间。
  • 您的数据库架构是什么,您是否定义了模型?
  • 我没有看到完整的字段!
  • 已更新以适合示例:是的,我有一个扩展 eloquent 的模型,这是我的迁移:public function up() { Schema::create('items', function(Blueprint $table) { $table-&gt;increments('id'); $table-&gt;string('item_name'); $table-&gt;boolean('completed'); $table-&gt;timestamps(); }); } 在我的模型中我有一个访问器 public function getCompletedAttribute($value) { return (boolean) $value; }
  • 我编辑了答案,我假设你的模型是 Skill 并且使用了 has_skill 属性。
猜你喜欢
  • 1970-01-01
  • 2015-09-16
  • 1970-01-01
  • 2011-06-17
  • 2011-05-23
  • 2020-12-20
  • 2023-03-03
  • 2017-04-30
  • 1970-01-01
相关资源
最近更新 更多