Returning same variable to every controller in laravel

laravel return view->with data
laravel pass data to view from controller
laravel view helper
create view in laravel 5 command
laravel 5.7 global variable
laravel controller common variable
global variable in laravel controller
laravel compact

I need to send the same result to almost every view page, so I need to bind the variables and return with every controller.

My sample code

public function index()
{
    $drcategory = DoctorCategory::orderBy('speciality', 'asc')->get();
    $locations = Location::get();

    return view('visitor.index', compact('drcategory','locations'));
}

public function contact()
{
    $drcategory = DoctorCategory::orderBy('speciality', 'asc')->get();
    $locations = Location::get();

    return view('visitor.contact', compact('drcategory','locations'));
}

But as you see, I need to write same code over and over again. How can I write it once and include it any function whenever I need?

I thought about using a constructor, but I cannot figure out how I can implement this.


You are able to achieve this by using the View::share() function within the AppServicerProvider:

App\Providers\AppServiceProvider.php:

public function __construct()
{
   use View::Share('variableName', $variableValue );
}

Then, within your controller, you call your view as normal:

public function myTestAction()
{
    return view('view.name.here');
}

Now you can call your variable within the view:

<p>{{ variableName }}</p>

You can read more in the docs.

global variable for all controller and views, To get the same data in any controller you may use: In Laravel, 5+ you can create a file in the config folder and create variables in that and This would only make a database call every 60 minutes otherwise it would return the cached value  This would only make a database call every 60 minutes otherwise it would return the cached value whenever you call Settings::getSettings(). share | improve this answer | follow | | | | edited Aug 7 '14 at 18:47


There are a few ways to implement this.

You can go with a service, a provider or, like you said, within the constructor.

I am guessing you will share this between more parts of your code, not just this controller and for such, I would do a service with static calls if the code is that short and focused.

If you are absolutely sure it is only a special case for this controller then you can do:

class YourController 
{

    protected $drcategory;

    public function __construct() 
    {

       $this->drcategory = DoctorCategory::orderBy('speciality', 'asc')->get();

    }

   // Your other functions here

}

In the end, I would still put your query under a Service or Provider and pass that to the controller instead of having it directly there. Maybe something extra to explore? :)

Views - Laravel, and separate your controller / application logic from your presentation logic. In this case, we are passing the name variable, which is displayed in the view using @return void */ public function register() { // } /** * Bootstrap any application  The most concise screencasts for the working developer, updated daily. There's no shortage of content at Laracasts. In fact, you could watch nonstop for days upon days, and still not see everything!


For this, you can use View Composer Binding feature of laravel

add this is in boot function of AppServiceProvider

    View::composer('*', function ($view) {
                $view->with('drcategory', DoctorCategory::orderBy('speciality', 'asc')->get());
                $view->with('locations', Location::get());
            }); //please import class...

when you visit on every page you can access drcategory and location object every time and no need to send drcategory and location form every controller to view.

Edit your controller method

public function index()
{
    return view('visitor.index');
}

Views & Responses - Laravel, Returning A Redirect To A Controller Action With Parameters In the example above the variable $name would be accessible from the view, and would contain Steve . If you wish You may also share a piece of data across all views: You may use the composers method to register a group of composers at the same time: Lets say I have a UserController with functions like "index" and "showProfile". These two functions I want to use with different views so all I want them to do is return some variables? How can I assign this function to a view in the route and just get the data I need? Seems like the correct way so I can reuse functions correct?


@Sunil mentioned way View Composer Binding is the best way to achieve this.

HTTP Controllers - Laravel, All Laravel controllers should extend the base controller class included with the default @param int $id * @return Response */ public function showProfile($id)​  I think this whole environment variable issue is representative of Laravel and its official packages (especially Cashier and Spark). It looks elegant at first glance but when you scratch the surface you find these types of ridiculous design decisions.


Controllers - Laravel, Instead of defining all of your request handling logic as Closures in route files, you may @param int $id * @return View */ public function show($id) { return  You may also generate redirects to controller actions. To do so, pass the controller and action name to the action method. Remember, you do not need to specify the full namespace to the controller since Laravel's RouteServiceProvider will automatically set the base controller namespace: return redirect()->action('[email protected]');


How to do use variable as global in Controller ?, I want to make global variables in same controller and use it in different functions. $this->cart->products()->sync($product,false); return back() (I'm new, so I don​'t have much information about laravel) All rights reserved. Think of this code as saying, "Hey Laravel, when you need an instance of Post, I want you to use my mocked version." Because the app extends the Container, we have access to all IoC methods directly off of it. Upon instantiation of the controller, Laravel leverages the power of PHP reflection to read the typehint and inject the dependency for you.


Laravel: Up and Running: A Framework for Building Modern PHP Apps, Three ways to load a view() There are three different ways to return a view. with view(), but if you ever see View::make(), it's the same thing, and you could also inject will work fine if the view doesn't rely on any variables from the controller. Introducing View Components in Laravel, an alternative to View Composers May 29, 2018 / Jeff In software development, one of the “best practices” is to create reusable code that can be implemented in different parts of your application if needed.