Put it int the construct function of your controller
public function __construct() {
$this->companies=...
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Let's assume that I have a controller which is dedicated to control the views, so i have different types of views and i send to them different variables, but - i have one query which repeats itself across all the functions, in this case its
$companies = Company::where('enabled',1)->get()->sortByDesc('score');
What would be the right approach to keep it DRY? Is it safe to put this variable in the external scope of the controller and not in every function?
Use a view composer for that and sort your query in the database
$companies = Company::where('enabled',1)->orderByDesc('score')->get();
Please or to participate in this conversation.