use Illuminate\Http\Request; $request = Request::create('/some/url', 'GET', [], [], [], ['HTTP_ACCEPT' => 'application/json']); if ($request->wantsJson()) { return response()->json(['message' => 'This is a JSON response']); } else { return view('some.view'); }
if ($request->wantsJson() && $request->input('type') == 'users') { $users = User::all(); return response()->json($users); } else { return view('some.view'); }In this example, we check if the request wants JSON and if the 'type' parameter is set to 'users'. If both are true, we retrieve a list of users from the database and return a JSON response. The Illuminate\Http\Request class is part of the Illuminate\Http package library in Laravel.