im trying to setup a simple blog with Angular-Frontend and Laravel-Backend.
But i cant manage to wrap my head around how to post something. It seems like every tutorial does it differently and i just cant follow.
What I want: Send Data to Laravel which should save it into 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 dont understand how i should send my data to laravel. It should contain two values, header and text. But of course right now nothing happens.
Can anybody give me a hint where I am thinking wrong?
I actually solved the problem but i dont know how.
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');
$request seemed to work in all my tests but not when send from Angular. Im really not sure why that is.