Posting JSON To Laravel
The "Trying to get property of non-object" error is a classic symptom of a mismatch between how the client sends data and how the server interprets it. In this specific scenario, you are sending a valid JSON payload via AngularJS, but your Laravel controller is treating the request body as if it were an empty string or raw text rather than structured JSON. This disconnect usually stems from missing configuration that tells Laravel to parse the incoming stream.
The Core Misunderstanding: Raw Input vs. Parsed Data
When you call Input::all() in Laravel, you are requesting a collection of input data. If your route does not explicitly instruct Laravel to expect JSON, the framework defaults to treating the request body as raw text (a string). Consequently, $data becomes a string containing {"id":"...", "profileUrl":"..."} instead of an associative array.
Attempting to access $data->id on a string throws the exception you are seeing because strings do not support property access in PHP. You cannot simply assume that sending JSON automatically triggers parsing; the server must be explicitly told to decode that stream.
Enabling JSON Parsing via Accept Headers
The most robust way to handle this is by leveraging HTTP headers. Your AngularJS client sends an Accept: application/json header, which is a strong hint, but Laravel's default behavior often requires explicit instruction on the input side.
You can resolve this by adding middleware to your route or globally telling Laravel to parse JSON inputs automatically. In older versions of Laravel (pre-5.4), you would typically register Input::facade('json') logic via a global filter or middleware. However, the modern and cleaner approach is using the AcceptsJson interface or explicitly parsing the input within your controller if headers are set correctly.
If you are on a version prior to Laravel 5.4, ensure you have added the following to your App::start() callback in app/start/global.php:
// Parse JSON content automatically when 'application/json' is accepted
Input::extend('json', function ($value) {
if (empty($value)) return array();
$data = json_decode($value, true);
if (null === $data) {
throw new \Illuminate\Http\Exceptions\HttpException("Invalid JSON");
}
return $data;
});
Alternatively, you can force the parsing inside your controller method by checking the content type. However, relying on headers is preferred for scalability.
Handling the Content-Type Header
Another critical piece of the puzzle is the Content-Type header. While AngularJS might be sending application/json in the Accept header, it must also declare what the body contains in the Content-Type header. If your client sends Content-Type: application/json, Laravel's default JSON middleware (available in modern versions) will automatically decode the input stream into an array when you call $request->all().
If you are using the request facade directly, switch from Input::all() to $this->request->all(). The Request object is more robust and handles content-type detection better than the deprecated Input facade.
Validating the Payload
Once parsing is handled, your code should validate the structure before accessing properties. This prevents runtime errors if a client sends malformed data or missing fields. Use Laravel's validation facade to ensure id and profileUrl exist before attempting to access them.
public function handle_ajax() {
$validated = $this->request->validate([
'id' => 'required|string',
'profileUrl' => 'required|url'
]);
// Now $validated is guaranteed to be an array or object
Clockwork::info($validated['id']);
// Or if using the request object directly:
// Clockwork::info($this->request->get('id'));
}
Migrating from Facades to Request Objects
The source code provided uses Input::all(). This facade is deprecated and removed in newer Laravel versions. For a long-term solution, refactor your controller to use the $request property injected via dependency injection or accessed directly via the request() helper function. This not only fixes the parsing issue but also future-proofs your application against API changes.
Conclusion
The error occurs because Laravel is receiving a raw string and trying to access object properties on it. By ensuring the Content-Type header is set correctly and updating your input handling logic to use the Request facade or proper middleware, you bridge the gap between AngularJS and Laravel. Always validate incoming data before accessing its properties to maintain application stability.
