Laravel unable to retrieve angular POST data

时间:2015-12-04 21:17:50

标签: angularjs laravel post laravel-5 http-post

I am submitting my form data through angular controller. The data then needs to be passed to Laravel Controller and stored in database. But, Laravel does not get the data and the values are NULL

Form

<form>
    <div class="form-group">
        <label for="username">Your Name</label>
        <input type="text" class="form-control" id="username" name="username" placeholder="Your Name" ng-model="user.name">
    </div>

    <div class="form-group">
        <label for="useremail">Email</label>
        <input type="text" class="form-control" id="useremail" name="useremail" placeholder="Email" ng-model="user.email">
    </div>

     <div class="form-group">
         <label for="username">Phone</label>
         <input type="text" class="form-control" id="userphone" name="userphone" placeholder="Phone" ng-model="user.phone">
     </div>

     <div class="form-group">
        <label for="userpassword">Password</label>
        <input type="password" class="form-control" id="userpassword" name="userpassword" placeholder="Password" ng-model="user.password">
      </div>

       <button type="submit" class="btn btn-default" ng-click="addUser(user)">Submit</button>

app.js

$http.get('http://api.app.mywebsite.com/users').success(function(data){
    $scope.user = data;

});

$scope.addUser = function(user){
console.log($scope.user);
$http({
    method: 'POST',
    url: 'http://api.app.mywebsite.com/users',
    dataType: 'json',
    data: $scope.user,
    headers: {'Content-Type': 'application/x-www-form-urlencoded' }
}).then(function(result){
    console.log(result);
}, function(error){
    console.log(error);
})

}

routes.php

Route::group(['middleware' => 'cors'], function()
{
    Route::resource('users', 'UserController');
});

UserController.php store function

public function store(Request $request)
{
    $user = new User;

    $user->name = $request->input('name');
    $user->email = $request->input('email');
    $user->phone = $request->input('phone');
    $user->password = $request->input('password');
    $user->save();

}

1 个答案:

答案 0 :(得分:2)

试试这个:

$inputs = Request::json()->all();
$user->name = $inputs['name'];