Laravel call a function after view is rendered

I am working on a laravel website that has the basic functionality of searching for flights/hotels etc..
One thing I want to do is show a loading page after the user submits the search information so that some animation is happening while the API runs.

Problem is I can’t figure out a way to show the page and run the controller code at the same time. I’ve been trying from my PagesController to pass the FlightsController to the view

PagesController.php

public function flight_search( Request $request )
{

    $FlightsController = new FlightsController();

    return view('flights.flight-search', [
        'FlightsController' => $FlightsController,
        'user_input' => $request
        ]);
}

and then calling the function in the flight-search view like so

flight-search.blade.php

@extends('app')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="page-heading">Searching for available tickets...</div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="progress">
                <div class="progress-bar progress-bar-striped progress-bar-warning active" role="progressbar" aria-valuemin="0" aria-valuemax="100" style="width:100%">
                </div>
            </div>
        </div>
    </div>
</div>

{{ $FlightsController->search( $user_input ) }}

@endsection

But the page load always waits for the function call to finish to complete, and the search() method itself returns the next view with the search result.

Any suggestions on how I should proceed with this?


Source: laravel

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.