I have the following public function in my controller, in this public function I have written another function that converts the $request that I receive in lowercase and add snake case, my question is, is it good practice to have this function inside my public function?
public function store(Request $request)
{
function convertSnake($data) {
$snake_case = str_replace(" ", "_", strtolower($data));
return $snake_case;
}
............ Más código ...........
}
The solution, despite working , I consider is not optimal, analyze this question:
What if you later need that conversion function somewhere other than the function
score
?Given the above, consider having these methods centralized so that you can call them later.
On the other hand, you do not need ( although it is not incorrect ) the native functions of the language to solve what you are looking for, the framework has several helpers that simplify your work.
Managing to have a solution as simple as this:
It is not the most optimal if you define a function F1 within another function F2 you can only call it within the function F1 , there are special cases that call the same function in other methods or controllers, for example calculates the VAT of a product or calculates a series of complex operations
I am going to show you how to make the calls of other functions
we create a file in ImportaRequestItem.php in app\Imports\ImportaRequestItem.php
in the controller we are going to import the file