if ($request->wantsJson()) { return response()->json(['message' => 'Success']); } else { return view('home'); }
if ($request->wantsJson()) { $data = ['name' => 'John', 'age' => '25']; return response()->json($data); } else { $data = ['name' => 'John', 'age' => '25']; return view('profile', $data); }In this example, if the request expects a JSON response, a JSON response with a user's profile data will be returned. If not, the user will be shown the profile page with the data passed to the view. The `wantsJson` method is a part of the Laravel framework package library in PHP. It is located in the `Illuminate\Http\Request` class.