Search code examples
phplaravellaravel-5.1

Refresh a content page without refreshing master page in laravel


I have an admin layout

enter image description here


I have a master layout contain my :

  • top nav
  • left side nav

Each page is a view, I extend the master layout view, and update only the content section.

My goal is to refresh only a content page without refreshing master page.


Is there any laravel package for something like this ?


Solution

  • Short answer: use AJAX. There no real need to learn a JavaScript framework if it's that simple (unless you really want to). The below example uses jQuery and assumes you're clicking on the Users link in the navigation (all links there should have a nav-item class so they can be easily identified via that selector) and the link has the attribute href="/admin/users". When clicked it loads the contents in the element with the class content:

    $('.item-nav').click(function (event) {
        // Avoid the link click from loading a new page
        event.preventDefault();
    
        // Load the content from the link's href attribute
        $('.content').load($(this).attr('href'));
    });
    

    If the route admin/users would return the users table with it's contents:

    get('admin/users', function () {
        return view('users')->with('users', Users::all());
    });
    

    The above Laravel route example is of course simplified to showcase the idea that each item in your navigation should link to the contents that need to be displayed dynamically.