Post data from Angular to Laravel

im trying to set up a simple blog with Angular-Frontend and Laravel-Backend.

But I can't wrap my head around how to post something. Each tutorial seems to do it differently and I just can't seem to follow.

What I want: send data to Laravel, which should store it in the database.

Angular factory

angularBlog.factory('blogFactory', function($http){

    var blogFactory = {};
    var api = 'http://localhost:8888/blogApi/public/api/';


    blogFactory.getPosts = function() {
        var result2 = $http.get(api + 'getposts');
        return result2;
    };

    //
    // POST FUNCTION
    //
    blogFactory.postPost = function(data) {
        return $http ({
            method: 'POST',
            url: api + 'post',
            headers: { 'Content-Type' : 'application/x-www-form-urlencoded' },
            data: $.param(data)
        });

    };

    return blogFactory;



});

      

Angular Controller

angularBlog.controller('blogController',['$scope','$http','blogFactory', function($scope, $http, blogFactory){



    $scope.getAllPosts = function() {
        blogFactory.getPosts()
            .success(function(result){
                $scope.posts = result;
            })
            .errror(function(){
                $scope.status = 'cant load data';
            })

    };

//
//  INSERT POST FUNCTION
//
    $scope.insertPost = function() {
        var data = {
            'header': $scope.header,
            'text'  : $scope.htmlVariable
        };
         blogFactory.postPost(data);
         alert('testalert');
    }

}]);

      

So, inside Laravel im doing:

    class blogController extends Controller
    {
        /**
         * Display a listing of the resource.
         *
         * @return Response
         */
        public function index()
        {
            return blogPost::get();

        }

//
// STORE FUNCTION
//

       public function store(Request $request)
    {
        $post = new blogPost();
        $post->header = $request->input('header');
        $post->text   = $request->input('text');

        $post->save();


    }

      

I just don't understand how I am supposed to send my data to laravel. It must contain two values: title and text. But, of course, nothing is happening now.

Can anyone give me a hint where I am going wrong?

+3


source to share


1 answer


I actually solved the problem, but I don't know how to do it.

In my LaravelController, I changed

$post->header = $request->input('header');
        $post->text   = $request->input('text');

      



to

 $post->header = Input::get('header');
        $post->text   = Input::get('text');

      

The $ request worked in all my tests, but not when sent from Angular. I'm really not sure why this is the case.

-1


source







All Articles