Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 399
code-8 Avatar asked Nov 03 '15 18:11

code-8


2 Answers

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.

like image 138
Bogdan Avatar answered Nov 10 '22 13:11

Bogdan


You can use renderSections().

In your main controller action ( say AdminUsersController@getIndex ) you can render a particular section of your view depending on the request. This way if you do an ajax request to the same page you are on you can return only the section you require.

 if($request->ajax()) {
         return view('admin.users',$users)->renderSections()['content'];
 }

 return view('admin.users',$users);
like image 24
Leo Avatar answered Nov 10 '22 15:11

Leo