Q:

Global Value in laravel 5.6

Here is my code ...
public function index()
{
    $clientIP = request()->ip();
    $cats=DB::table('catagory')->get();
    $news=DB::table('news')->orderby('nID','desc')->limit('5')->get();
    $books=DB::table('book')->where('bPrice','=',0)->orderby('bID','desc')->limit('5')->get();
    return view('index',compact('clientIP','cats','news','books'));
}

As you see there are some variables, I passed them to my view but I have to repeat them in every view I wanna show.Is there any way to globalize them and use anywhere.Please help with sample code.Thanks.

A:

Laravel view composer may help you. 
You should add this code to your app/Providers/AppServiceProvider.php.
    view()->composer(['index', 'index1', 'index2'], function($view) {
        $clientIP = request()->ip();
        $cats=DB::table('catagory')->get();
        $news=DB::table('news')->orderby('nID','desc')->limit('5')->get();
        $books=DB::table('book')->where('bPrice','=',0)->orderby('bID','desc')->limit('5')->get();
        $view->with([
            'clientIP' => $clientIP,
            'cats' => $cats,
            'news' => $news,
            'books' => $books,
        ]);
    });

