1

i have this code in controller

$Headercategory=Categories::where('status','1')->orderby('order')->get();
return view('front.home',['Headercategory'=>$Headercategory]

all my controlles will include "Headercategory" that mean i have to do same code with all controllers i have.

is there any way to make this code public in all project? without no need to add it in all controllers

Ahmed Saad
  • 53
  • 8
  • you need scopes [docs](https://laravel.com/docs/8.x/eloquent#writing-global-scopes). Here are some examples [link1](https://stackoverflow.com/questions/44098385/using-multiple-laravel-scopes-in-or-context), [link2](https://stackoverflow.com/questions/63010974/gracefully-exiting-laravel-scopes), [link3](https://stackoverflow.com/questions/29913412/laravel-global-scopes-with-joins) – Jairo Nava Magaña Feb 16 '21 at 18:34
  • 1
    is this a particular view that is rendering the menu? if so you could use a view composer to pass that data for that particular view ... if not you could use a view share – lagbox Feb 16 '21 at 18:40

1 Answers1

0

As I understand, you want to pass categories variable in your all blade file. You can do that easily from /app/Providers/AppServiceProvider.php on boot method :

use View;

public function boot()
{
     $Headercategory = Categories::where('status','1')->orderby('order')->get();

     View::share(['Headercategory' => $Headercategory);
}

Now you can access $Headercategory anywhere from your blade

STA
  • 30,729
  • 8
  • 45
  • 59