Use $request->expectsJson() in hybrid controllers to serve formatted JSON responses to API/AJAX requests and redirects to standard web browsers.
When building controllers shared between traditional Blade views and AJAX/Inertia/API clients (such as login, search, or deletion endpoints), branching based on client request headers is standard practice.
$request->expectsJson() inspects Accept and X-Requested-With headers automatically.
Hybrid Controller Action
use App\Models\Post;
use Illuminate\Http\Request;
public function destroy(Request $request, Post $post)
{
$post->delete();
// If client requested JSON (API / AJAX / Mobile app)
if ($request->expectsJson()) {
return response()->json([
'success' => true,
'message' => 'Post deleted successfully.',
]);
}
// Standard web browser form submission
return to_route('posts.index')
->with('success', 'Post deleted.');
}
What expectsJson() Inspects
- Checks if the
Acceptheader containsapplication/json. - Checks if the request is an AJAX call via
X-Requested-With: XMLHttpRequest.
Summary
- Reusable pattern for multi-channel hybrid controllers.
- Eliminates duplicate controller actions for web and API endpoints.
- Built-in to all Laravel request instances.
Related Tips
View all tips →Authenticate External API Requests Cleanly with withToken()
Use withToken() on the Laravel HTTP client to attach Authorization Bearer tokens without manual header string formatting.
Throw Exceptions on HTTP Client Errors with throw() and throwIf()
Use throw(), throwIf(), and throwUnless() on Laravel HTTP client responses to handle 4xx and 5xx API errors with standard exception workflows.