0

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');
    }
3
  • 1
    use this php function - w3schools.com/php/func_string_strtoupper.asp Commented Apr 22, 2021 at 7:38
  • @Hamelraj where would it go though Commented Apr 22, 2021 at 7:48
  • 2
    $energy->energyid = strtoupper($request->input('energyid')); Commented Apr 22, 2021 at 7:52

2 Answers 2

1

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');
}

}
Sign up to request clarification or add additional context in comments.

Comments

0

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');
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.