php - Making data save in uppercase with laravel

When I am entering data on my form, I have set it to only enter uppercase letters however when saving, I see that it saves as a lowercase in the SQL DB. Is there any way to change this?
Heres my code:
controller:
public function store(Request $request)
{
$energy = new Energy;
$energy->energyid = $request->input('energyid');
$energy->energydetail = $request->input('energydetail');
$energy->save();
return redirect('/page')->with('success', 'data added');
}
Answer
Solution:
You can use validation hopefully it will solve your problem
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'//your_field' => 'required|string|regex:/(^([A-Z]))/u'
]);
if ($validator->fails()){
return "invalid data";
}
else{
$energy = new Energy;
$energy->energyid = $request->input('energyid');
$energy->energydetail = $request->input('energydetail');
$energy->save();
return redirect('/page')->with('success', 'data added');
}
}
Answer
Solution:
There are a couple of ways with which you can achieve this, one is as mentioned by Doggo$energy->energyid = strtoupper($request->input('energyid'));
and the other is creating a request file and using a package (https://github.com/Waavi/Sanitizer)
here is how your code will look like
class EnergyStoreRequest extends BaseFormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'energyid' => 'required',
'energydetail' => 'required'
];
}
/**
* Filters to be applied to the input.
*
* @return array
*/
public function filters()
{
return [
'energyid' => 'trim|uppercase',
];
}
}
And your controller will look likt this
public function store(EnergyStoreRequest $request)
{
$input = $request->validated();
$energy = new Energy;
$energy->energyid = $input['energyid'];
$energy->energydetail = $input['energydetail'];
$energy->save();
return redirect('/page')->with('success', 'data added');
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: dompdf image not found or type unknown
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.