Support the ongoing development of Laravel.io →
Views Blade Eloquent

I am using the Laravel framework for my PHP Webapp.

I am executing some SELECT queries which can take a few seconds to run. My Controller looks like this:

    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use App\Clans;
    use App\Http\Requests;
    use App\Http\Controllers\Controller;
    use DB;
    
    class ClansController extends Controller
    {
        public function index(){
    		\DB::connection()->disableQueryLog();
    	
    		$clan = Clans::paginate(100,['id', 'clanid', 'name', 'level', 'exp', 'warwinpercent', 'warswon', 'playercount', 'score']);
    		
    		\DB::connection()->enableQueryLog();
    		
    		
    		return view('clans.index')->with('clan', $clan);
    	}
    	
    	
    	
    	public function order($orderby){
    		\DB::connection()->disableQueryLog();
    		
    		if($orderby == "level"){
    			$clan = Clans::orderBy('level', 'DESC')
    			->orderBy('exp', 'DESC')
    			->paginate(100,['id', 'clanid', 'name', 'level', 'exp', 'warwinpercent', 'warswon', 'playercount', 'score']);
    		}elseif($orderby == "score"){
    			$clan = Clans::orderBy('score', 'DESC')
    			->paginate(100,['id', 'clanid', 'name', 'level', 'exp', 'warwinpercent', 'warswon', 'playercount', 'score']);
    		}elseif($orderby == "warwinpercent"){
    			$clan = Clans::orderBy('warwinpercent', 'DESC')
    			->where('warswon', '>=', '100')
    			->paginate(100,['id', 'clanid', 'name', 'level', 'exp', 'warwinpercent', 'warswon', 'playercount', 'score']);
    		}else
    			$clan = Clans::paginate(100,['id', 'clanid', 'name', 'level', 'exp', 'warwinpercent', 'warswon', 'playercount', 'score']);
    		
    		\DB::connection()->enableQueryLog();
    		
    		return view('clans.index')->with('clan', $clan);
    	}
    	
    	
    	public function showclan($id){
    		$clan = Clans::find($id);
    		
    		return view('clans.clan' , compact('clan'));
    	}
    	
    	
    }

What I am trying to do is while that is all running I want a loading animation to be shown to the user. When the page is loaded then I want the page to be shown.

How can I go about achieving this? I looked into JQuery and PHP stuff but they only show when the full page has already loaded (I tried them in the View).

Thanks for any help you can provide.

Last updated 3 years ago.
0

I don't know enough about Laravel to answer with authority on Laravel, but I suspect this is not a Laravel issue but an architecture / process issue.

My solution would be to load first before doing the heavy lifting, then load the required data using AJAX.

You can either return the results as JSON, and use JavaScript to populate your page, or just return a rendered view and insert that directly.

Last updated 9 years ago.
0

davestewart said:

I don't know enough about Laravel to answer with authority on Laravel, but I suspect this is not a Laravel issue but an architecture / process issue.

My solution would be to load first before doing the heavy lifting, then load the required data using AJAX.

You can either return the results as JSON, and use JavaScript to populate your page, or just return a rendered view and insert that directly.

How do I do that though?

0

Well, instead of running all the DB setup in your index controller:

Create a new route/method

  1. Set up a new route, e.g. 'clans/load'
  2. Route this to ClansController@load
  3. In the ClansController load() method, put all the DB code which is taking the time to load
  4. Return the results, in JSON or HTML

In your index method:

  1. Return just the basic HTML page

In your HTML page:

  1. Show the loading animation
  2. Using jQuery, at the end of the page (or on a page ready function) call the 'settings/load' endpoint
  3. When the results are returned, stop the animation and inject your content

Something like this:

$.get('clans/load', function(html){
    $('#content').html(html);
});

You could even pass variables via the route to the load() method if you wanted, such as page, count, etc. and wire these to links in the HTML page.

Loads of advice out there on jQuery. If you don't know it, now is the time to learn!

Last updated 9 years ago.
0

I still don't get it... :/

0

Probably time to change jobs then.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

shivampaw shivampaw Joined 30 Oct 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.