I have an AJAX call and I want it to return my blade template with a query result.
My jQuery code:
$(document).on('click','.user-profile-child', function(){
let id = $(this).parents('.user-profile-parent').attr('id');
$.ajax({
url: '/loadUserProfile',
type: 'GET',
data: {id: id},
dataType: 'json',
success: function(r){
console.log(r);
}
})
});
My function which must execute the query and return the view with the results:
public function loadUserProfile(Request $request){
$id = $request->input('id');
$user = User::where('id',$id)->get()->toArray();
$returnHTML = view('userProfile')->with('user', $user)->render();
return response()->json(array('success' => true, 'html'=>$returnHTML));
}
This is what I get in the console https://ibb.co/dth85Kh
I have tried writing my function like this but it doesn't redirect to my view and doesn't return anything in success.
public function loadUserProfile(Request $request){
$id = $request->input('id');
$user = User::where('id',$id)->get()->toArray();
return view('userProfile',['user' => $user]);
}