Stacy J
Stacy J

Reputation: 2781

Laravel unable to retrieve angular POST data

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();

}

Upvotes: 1

Views: 1533

Answers (1)

Mina Abadir
Mina Abadir

Reputation: 2981

Try this out:

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

Upvotes: 3

Related Questions