What is the best way to remove duplicate codes from Laravel Controller? In my particular case, I have Blog Controller where are multiple functions for each of sub-pages (index page, about, contact, single post page...). In any of those functions I have some code which is repeated. Can I create a special function which then I could call into any of function?
class BlogController extends Controller {
public function getIndex() {
$blogs = Blog::orderBy('id', 'desc')->where('status', '1')->paginate(3);
return view('index-page')->withBlogs($blogs);
}
public function getAbout() {
$blogs = Blog::orderBy('id', 'desc')->where('status', '1')->paginate(3);
return view('about-page')->withBlogs($blogs);
}
}
And now, I want remove duplicate code with creating a special function (my code is only example, the real repeated code is much longer). Is that even possible? Is there some other way except creating another function? Maybe I can create something like function.php in Wordpress?