PHP code example of imanghafoori / laravel-widgetize

1. Go to this page and download the library: Download imanghafoori/laravel-widgetize library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

imanghafoori / laravel-widgetize example snippets


'providers' => [
    // for laravel 5.4 and below
    Imanghafoori\Widgets\WidgetsServiceProvider::class,
];

namespace App\Widgets;

class MyWidget
{
    // The data returned here would be available in widget view file automatically.
    public function data($my_param=5)
    {
        // It's the perfect place to query the database for your widget...
        return Product::orderBy('id', 'desc')->take($my_param)->get();

    }
}

expire_widgets(['someTag', 'tag1']);

    public function cacheKey($args)
    {
        return 'user_widget_'.$args['user_id'];
    }

namespace App\Widgets;

class MyWidget
{

    public function data()
    {
        $id = request('order_id'); // here we are using a request parameter to fetch database...
        return Product::where('order_id', $id)->get();
    }
    

    public function extraCacheKeyDependency()
    {
        // so the value of this parameter should be considered for caching.
        return request()->get('order_id');
    }
    
}


Route::widget('/some-url', 'MyWidget', 'MyRouteName1'); // <-- exposes HTML
// or
Route::jsonWidget('/my-api','MyWidget', 'MyRouteName2'); // <-- exposes json


Route::get('/api/products/{id}', '\App\Widgets\MyWidget@data');
 bash
php artisan vendor:publish
 bash
php artisan make:widget MySexyWidget
 php
public function compose(View $view)
{
    $view->with('count', $this->users->count());
}