I wonder if there are any differences between putting a function in helpers or just putting it on controllers? For example, a function to count how much data in database, In helpers the code looks like:
function countData($status = 'active')
{
$data = Models::where('status', 'like', $status)->count();
return $data;
}
And in controllers the code looks like:
$status = 'active';
$countData = Models::where('status', 'like', $status)->count();
return view('some.view', compact('countData');
Which one is the best, using helpers or controllers? Is there any way to check the performances between those two? Thanks.
CodePudding user response:
A controller method is to be used with an HTTP request. A helper can be used anywhere in the code As the same bootstrap is called if you are responding to an HTTP request. Both approaches are equally slow since you're querying the database. That adds the most time, therefore fiddling with micro-optimizations won't get you anywhere, it's a waste of effort.