1

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.

thisiskelvin
  • 4,136
  • 1
  • 10
  • 17
Delowar Hossain
  • 375
  • 1
  • 3
  • 19
  • 1
    Have you thought about creating a `trait` which you can add the functionality above when needed? – thisiskelvin Sep 27 '18 at 09:40
  • 1
    One of a simple way is: add value to variables named `$this->data["drcategory"]` and `$this->data["locations"]`, in your construction, before return to view, you can write like this `$data = $this->data;` and `return view('visitor.contact', compact('data'));`. You can add more variable into `data` array. – Ngoc Nam Sep 27 '18 at 09:48
  • do you want to share this variable with evey view (handles by other controllers) or juts the views handles by this controller? – Tharaka Dilshan Sep 27 '18 at 09:55
  • I want to share this variable to views of only this controller. – Delowar Hossain Sep 27 '18 at 10:06

4 Answers4

5

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.

thisiskelvin
  • 4,136
  • 1
  • 10
  • 17
shubham singh
  • 400
  • 2
  • 7
1

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? :)

Diogo Santo
  • 769
  • 6
  • 12
0

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');
}
Sunil kumawat
  • 804
  • 8
  • 14
0

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