There are questions like this on StackOverflow, that I know.
Trust me, I have tried all the solutions to no avail.
I have been trying to post this json data from angularjs to a laravel controller method:
[
{
"id": 2,
"name": "add-user",
"display_name": "Add new user",
"description": "Create a new worker",
},
{
"id": 4,
"name": "add-role",
"display_name": "Add role",
"description": "Add user role",
}
]
this is the code that posts this json data to a laravel controller:
$http({
method: 'POST',
url: '/api/roles/assignPermissions/' + name,
headers: { 'Content-Type' : 'application/json' },
data: $.param(role_perms)
}).success(function(data){
console.log(data);
});
Now in this controller method, I just want retrieve this json data, return it and display it in the console to be sure that I received it in the controller.
Below is the controller and all the attempts I've made to get this json data and return it.
The controller:
public function massAssignPerms(Request $request, $roleName)
{
$role = Role::where('name', $roleName)->first();
// this returned nothing
return json_decode(file_get_contents('php://input'),true);
// this returned: add-user=&add-role=
return file_get_contents('php://input');
// returned nothing
return json_decode($request->getContent(), true);
// returned empty array []
return $request->json()->all();
}
I am unable to find what's wrong with my code. Or it could be that I'm doing it wrongly?
Any help will be greatly appreciated.
dd($request);