How to Create Widgets in Laravel?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Now now in this post we will learn how to create our own widgets in laravel application. At first thing why we are create the widgets, So now we can use widget because when we can create the re-usable code, for example we have a manu items and it comes from the database as like home, items, products, about us, contact us etc. So now we need to display in top header, in sidebar and in footer. But in top we need to just display home and aboutus menu and in sidebar products and items menu and same that way in footer. So, now it is basically we need to display in three place in our site page. So in widgets through we can make one function or view and then need to call just one widget at time and it get difference manu. so on that’s why we need to use widgets. So this is for the example but we can use more.

Now in, the following example we can see, how to implement widgets packages and also how to make widgets in laravel and how to use in widgets view. So just need to follow step and use in your side widgets:

Step 1: Installation

So now open our composer.json file and need to add bellow line in required package.

"pingpong/widget" : "~2.0"

Then, need to run command composer update

So now open the config/app.php file and need to add service provider and aliase.

'providers' => [
	....

	'Pingpong\Widget\WidgetServiceProvider',

],

'aliases' => [

	....

	'Widget' => 'Pingpong\Widget\WidgetFacade'

],
Step 2: Create app/widgets.php

So now in this step need to create a new file widgets.php(app/widgets.php) in our app directory and need to put bellow code in that file.

class MunuBarWidget {
    public function namageMenu($value)
    {
        switch ($value) {
            case 'homepage':
                $html = '<ul>';
                $html = $html.'<li>Homepage 1</li>';
                $html = $html.'<li>Homepage 2</li>';
                $html = $html.'<li>Homepage 3</li>';
                $html = $html.'</ul>';
                break;
            case 'sidebar':
                $html = '<ul>';
                $html = $html.'<li>SideBar 1</li>';
                $html = $html.'<li>SideBar 2</li>';
                $html = $html.'<li>SideBar 3</li>';
                $html = $html.'</ul>';
                break;
            default:
                $html = '<ul>';
                $html = $html.'<li>Default 1</li>';
                $html = $html.'<li>Default 2</li>';
                $html = $html.'<li>Default 3</li>';
                $html = $html.'</ul>';
                break;
        }
        return $html;
    }
} 
Widget::register('menubar', 'MunuBarWidget@namageMenu');
Step 3: create route

In the next simple we need to create a route in our routes.php file:

Route::get('/', function () {
	return view('welcome');
});
Step 4: Use Widgets in view

Now we are in last step. So we can use widgets by using Widget facade. so we need to open welcome.blade.php(resources/views/welcome.blade.php) file and put bellow code:

<html>
    <head>
        <title>Laravel</title>
    </head>
    <body>
        <div class="container">
            <div class="content">
                <h1>HomePage</h1>
                {!! Widget::menubar('homepage') !!}
                <h1>SideBar</h1>
                {!! Widget::menubar('sidebar') !!}
            </div>
        </div>
    </body>
</html>

Read Also : Laravel and AngularJS CRUD with Search & Pagination Example

Thanks for read. I hope it help you. For more you can follow us on facebook.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →