1

I can access a function in every view like this.

In my AppServiceProvide Code

public function boot()
{
    $post = Post::latest()->first();
    View::share(compact('post'));
}

How can i access it in every controller?

What is the best way to call a function in every controller,as i need to make sure latest record from database.

Romnick Susa
  • 1,279
  • 13
  • 31
Khirad Zahra
  • 843
  • 2
  • 17
  • 42

1 Answers1

1
**You can write in model also using model object**

public function YourFunction(){
  return "data";
}

**call it in your controller like this make model object suppose your 
model name is YourModel in controller you can call like this**

$model = new YourModel();
$data = $model->YourFunction(); //calling method in controller
Kuldeep Mishra
  • 3,846
  • 1
  • 21
  • 26