Home > Software design >  Laravel Must be string when saving
Laravel Must be string when saving

Time:05-17

I have a mounted variable and an error occurs when i'm about to save the variable.

   public $code;

   public function mount()
    {
        $this->code = substr(str_shuffle(str_repeat("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 5)), 0, 3).Carbon::now()->format('md').rand(100, 999);
    }
protected function rules(): array
    {
        return [
            'applicant.code' => [
                'string',
                'required',
             ],
        ];
    }

blade

<div >
        <label  for="code" hidden>{{ trans('cruds.applicant.fields.code') }}</label>
        <input  type="text" name="code" id="code" value="{{ $code }}" disabled hidden>
        <div >
            {{ $errors->first('applicant.code') }}
        </div>
        <div >
            {{ trans('cruds.applicant.fields.code_helper') }}
        </div>
    </div>

<div >
        <button  type="submit">
            {{ trans('global.submit') }}
        </button>
        <a href="{{ route('admin.applicants.index') }}" >
            {{ trans('global.cancel') }}
        </a>
    </div>

The code must be a string.

I can't seem to figure out this error. Can anyone help?

CodePudding user response:

<input type="text" name="code" id="code" value="{{ $code }}" disabled hidden>

Your input code is disabled

CodePudding user response:

function mount() {
    $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';    
    $date = date("md");
    $numbers = rand(100, 999); 
   
    $randomString = substr(str_shuffle(str_repeat($characters, 5)), 0, 3). $date . $numbers;       
      
    return $randomString;
}
echo mount();
  • Related