How to pass link from controller to view in laravel on ajax call

Updated at 09-Sep-2021, By samar

How to pass link from controller to view in laravel on ajax call

Hello everyone, in this post we will examine how to solve the "How to pass link from controller to view in laravel on ajax call" programming puzzle.

You can pass link from controller to view in laravel using ajax call. You can get the URL link in controller using different method like route() and action() method which you want to send the view as an ajax response.
  • Pass link from controller to view in laravel using ajax

    //Create a route in web.php with name verification_link and after that call route method in controller and return to view using response()->json() method.
    $verificationLink =  route('verification_link', ['phone' => $user->phonenumber]);
    return response()->json([
        'msg' => 'Please Verify your email first then login <a href="'.  $verificationLink . '"> Verify Now </a>',
    ]);
    
    //Get the response from ajax using response.msg and append to html tag using jQuery .html() method.
    <div id="response"></div>
    <script>
    $.ajax({
        url: form.action,
        type: form.method,
        data: $(form).serialize(),
        success: function(response) {
            $('#response').html(response.msg).show();
        }            
    });
    </script>
    

Back to code snippet queries related laravel