Is there a way to pass a variable into an if statement of an Ajax request?

I am using Yajra Datatables to display audit data into a table. I have a function which gets audit data from a table using a received ID of a user and Request and gets the data using that ID. When i check if the request is Ajax using an if statement; I cannot get any variable outside of this if statement and even if i hard code a variable into this if statement so it should get the data based on this variable nothing is happening. I even tried die and dump the hard coded variable but its not dumping it on the screen. Below is the function;

    public function getByUser(User $user, Request $request)
{
    
    $email = $user->email;

       if ($request->ajax()) {
        //$email = "[email protected]";
        $audits = Audit::where('action_taken_by', $email)
        ->orderBy('id', 'DESC')
        ->get();

            return Datatables::of($audits)
                    ->addIndexColumn()
                    ->rawColumns(['action'])
                    ->make(true);
        }

    return view('audit/index');
}

If i remove the if statement the code is working but just dumping the array on the screen and not returning them on the view as intended and if the if statement is there all the results are displayed without checking the email variable. Is there a way i can pass this variable into the if statement?