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

Created at 10-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

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.