2017-01-01 90 views
0

這是我的形式方法與laraval用於PUT方法

<form class="form-horizontal alert alert-info" id="editForm" ng-submit="updateComment(currentComment)" hidden> 
      <h3 class="text-center">Update Comment Details</h3> 
      <div class="form-group"> 
       <label for="Name">Author Name:</label> 
       <input type="text" class="form-control" ng-model="currentComment.author" value="{{currentComment.author}}"> 
      </div> 
      <div class="form-group"> 
       <label for="text">Comment Text:</label> 
       <input type="text" class="form-control" ng-model="currentComment.text" value="{{currentComment.text}}"> 
      </div> 
      <input name="_method" type="hidden" value="PUT"> 
      <div class="form-group"> 
       <button class="btn btn-warning" ng-disabled="" ng-click="">Update</button> 
       <button class="btn btn-warning" ng-disabled="" ng-click="HideEditForm()">Cancel</button> 
      </div> 
     </form> 

這是我的服務

angular.module('commentService', []) 

    .factory('Comment', function($http) { 

     return { 
      get : function() { 
       return $http.get('api/comments'); 
      }, 
      show : function(id) { 
       return $http.get('api/comments/' + id); 
      }, 
      save : function(commentData) { 
       return $http({ 
        method: 'POST', 
        url: 'api/comments', 
        headers: { 'Content-Type' : 'application/x-www-form-urlencoded' }, 
        data: $.param(commentData) 
       }); 
      }, 
      update : function(commentData) { 
       return $http({ 
        method: 'PUT', 
        url: 'api/comments', 
        headers: { 'Content-Type' : 'application/x-www-form-urlencoded' }, 
        data: $.param(commentData) 
       }); 
      }, 
      destroy : function(id) { 
       return $http.delete('api/comments/' + id); 
      } 
     } 

    }); 

,這是我的角的js控制器

angular.module('mainCtrl', ['datatables']) 

    .controller('mainController', function($scope, $http, Comment) { 
     // object to hold all the data for the new comment form 
     $scope.commentData = {}; 

     // loading variable to show the spinning loading icon 
     $scope.loading = true; 

     // get all the comments first and bind it to the $scope.comments object 
     Comment.get() 
      .success(function(data) { 
       $scope.comments = data; 
       $scope.loading = false; 
      }); 


     // function to handle editing the form 
     $scope.currentComment = {}; 
     $scope.editForm = function(id){ 
      Comment.show(id).success(function(data){ 
       $scope.currentComment = data; 
       $('#empForm').slideUp(); 
       $('#editForm').slideToggle(); 
      }); 

     }; 

     $scope.updateComment = function(commentData){ 
      Comment.update(commentData).success(function(data){ 
      }); 
     }; 

     //hide edit form 
     $scope.HideEditForm = function(){ 
      $('#editForm').slideToggle(); 
     }; 
     // function to handle submitting the form 
     $scope.submitComment = function() { 
      $scope.loading = true; 

      // save the comment. pass in comment data from the form 
      Comment.save($scope.commentData) 
       .success(function(data) { 
        $scope.commentData = {}; 
        // if successful, we'll need to refresh the comment list 
        Comment.get() 
         .success(function(getData) { 
          $scope.comments = getData; 
          $scope.loading = false; 
         }); 

       }) 
       .error(function(data) { 
        console.log(data); 
       }); 
     }; 

     // function to handle deleting a comment 
     $scope.deleteComment = function(id) { 
      $scope.loading = true; 

      Comment.destroy(id) 
       .success(function(data) { 

        // if successful, we'll need to refresh the comment list 
        Comment.get() 
         .success(function(getData) { 
          $scope.comments = getData; 
          $scope.loading = false; 
         }); 

       }); 
     }; 

    }); 

和這是我的控制器

<?php 

class CommentController extends \BaseController { 

    /** 
    * Send back all comments as JSON 
    * 
    * @return Response 
    */ 
    public function index() 
    { 
     return Response::json(Comment::get()); 
    } 

    /** 
    * Store a newly created resource in storage. 
    * 
    * @return Response 
    */ 
    public function store() 
    { 
     Comment::create(array(
      'author' => Input::get('author'), 
      'text' => Input::get('text') 
     )); 

     return Response::json(array('success' => true)); 
    } 

    /** 
    * update a resource in storage. 
    * 
    * @return Response 
    */ 
    public function update() 
    { 
     Comment::where('id', Input::get('author'))->update(['author'=>Input::get('author'), 'text'=>Input::get('text')]); 

     return Response::json(array('success' => true)); 
    } 

    /** 
    * Return the specified resource using JSON 
    * 
    * @param int $id 
    * @return Response 
    */ 
    public function show($id) 
    { 
     return Response::json(Comment::find($id)); 
    } 

    /** 
    * Remove the specified resource from storage. 
    * 
    * @param int $id 
    * @return Response 
    */ 
    public function destroy($id) 
    { 
     Comment::destroy($id); 

     return Response::json(array('success' => true)); 
    } 

} 

所有的方法工作,除了put方法我是新來的角請幫我的問題在此先感謝

回答

0

對於你的情況,你可以使用理清,但你應該添加新字段名爲_method = PUT您的要求(commentData

enter image description here

+0

是的,我知道我可以使用後,但我想使用REST API約定... – hu7sy

+0

則必須使用X WWW的形式,進行了urlencoded,而不是形式 - 數據,那個ê其他答案 –

+0

我使用它的兄弟你可以看到我的更新功能在服務 – hu7sy