Mohammed
Mohammed

Reputation: 658

how to receive JSON data from angularjs into laravel

Help me on this:

I am sending JSON data through angularjs to laravel :

My angularjs Json code like:

        $scope.addnew = {name:'',email:'',message:''};
        $scope.addnew.name=$scope.name;
        $scope.addnew.email=$scope.email;
        $scope.addnew.message=$scope.message;

          $http.post("url",$scope.addnew)
        .then(function mysuccess(response) {

           console.log(response.data);

          });

Note: I have predefined url in my file.

And I want to receive this JSON file in my laravel controller so that all data can be saved in my mysql table "contactemail" and fields of table are named as name for name, email for email and message for message of user. As I am newbie in Laravel I am not able to do this correctly.Please Help me in this.

Upvotes: 0

Views: 505

Answers (1)

ssuhat
ssuhat

Reputation: 7656

You can retrieve your input at Laravel using $request->input('your_json_key').

Example basic saving data:

$post = new Post();
$post->title = $request->input('title');
$post->save();

Those above code will create new post and save it to posts table (depends on your model setup).

For more information: https://laravel.com/docs/5.3/requests#accessing-the-request

Upvotes: 1

Related Questions