Define a function which will return the data which you wanted to use on every view page.
Like I defined my function in ProductServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?php
namespace App\Providers;
use App\Models\Products;
class ProductServiceProvider extends BaseServiceProvider {
public function __construct() {
}
public static function getTrendingProducts(){
$products = Products::select('id', 'product_title', 'price', 'discount','views_count')->orderBy('views_count','desc')->get();
}
}
|
Now call getTrendingProducts() function in AppServiceProvider.php which is under App/Providers direcory.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Providers\ProductServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$trandingProducts = ProductServiceProvider::getTrendingProducts();
view()->share('trandingProducts', $trandingProducts);
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
|
Now you will be able to use $trandingProducts variable on every view of application.