1

I have a simple code here and want to pass data to the laravel controller so I can store it to the database. My codes are:

AccountController.php

public function store(Request $request)
    {
        Account::create(array(
            'email' => $request->get('email'),
            'name' => $request->get('text'),
            'age' => $request->get('age'),
        ));

        return ['success' => true];
    }

Blade

<form ng-submit="newAccount()">
          <div class="form-group">
            <label for="email">Email address</label>
            <input type="email" class="form-control" id="email" ng-model="accountData.email">
          </div>

          <div class="form-group">
            <label for="fullname">Full Name</label>
            <input type="email" class="form-control" id="fullname" ng-model="accountData.name">
          </div>

          <div class="form-group">
            <label for="age">age</label>
            <input type="email" class="form-control" id="age" ng-model="accountData.age">
          </div>
</form>

app.js

var app = angular.module('accountsApp', []);

app.controller('accountsCtrl', function($scope, $http) {

 $scope.newAccount = function() {
      //add data
      $http.post('/api/accounts', 
      {
        //what will I put here to get the textbox values?
      }).
      .success(function(response) {
        scope.accounts = response;
      })
      .error(function(response) {
        console.log(response);
      });

    };

});

As you can see in my app.js, I'm stuck on how to get the data from the textbox in my blade. Is there an easy way to do this? Thank you.

2
  • why dont u use jquery for such a simple thing? you want to send it to the controller via ajax or via the submit form action Commented Sep 26, 2015 at 10:43
  • 1
    because I want to learn Angular @patricio Commented Sep 26, 2015 at 11:32

1 Answer 1

1

Pretty easy, you can just add an object to pass with the POST request. Laravel will pick these variables up 1 to 1.

var app = angular.module('accountsApp', []);

app.controller('accountsCtrl', function($scope, $http) {

 $scope.newAccount = function() {
      //add data
      $http.post('/api/accounts', 
      {
        email: $scope.accountData.email,
        text: $scope.accountData.text,
        age: $scope.accountData.age

      }).
      .success(function(response) {
        scope.accounts = response;
      })
      .error(function(response) {
        console.log(response);
      });

    };

});
Sign up to request clarification or add additional context in comments.

3 Comments

Then you might have another probably, maybe you're hitting Laravel's mass assignment exception. Check your network tab in your console what's happening.
I see the problem now, the $scope.accountData.email doesn't do it. It must be something. I tried a static value and it works. for example email: 'test'
@FewFlyBy Sorry for the late response, I see what you're doing wrong, all the form types are <input type="email" this should be type="text" otherwise it won't validate and won't send!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.