David Orizu's avatar

Laravel Str::random() in $data

HELLO Everyone I'm trying to give a column in my user table a Str::random(8).time(). value. Here is my code

    protected function validator(array $data)
    {

        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'username' => ['required ', 'string' ,'max:255' ,'unique:users'],
            // 'package' => ['required'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
            'member_id' => ['required', 'string', 'max:255'],
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\Models\User
     */
    protected function create(array $data)
    {

        dd($data);

         $member = Str::random(8).time();

        $user =  User::create([
            'name' => $data['name'],
            'username' => $data['username'],
            // 'package' => $data['package'],
            'email' => $data['email'],
            'member_id' => $data[$member],
            'password' => Hash::make($data['password']),
        ]);
        return   $user;
    }

And I didn't forget to add

use Illuminate\Support\Str;

To my controller.

So when I submit my form nothing happens the page just reloads. And I no the problem is from that 'member_id' that I want to allocated Str::random(8).time() to. Please you guys should help me out I'll really be grateful. Thanks

0 likes
8 replies
Snapey's avatar
 'member_id' => $member,

assuming your column can store 18 characters and it is in the $fillable array

Snapey's avatar

"didn't work" is absolutely no help in determining the problem

1 like
Snapey's avatar

If the page 'just reloads' then it is failing validation, which is a bit of a puzzle because I don't see where you perform validation

David Orizu's avatar

@Snapey

              <form action="{{ route('register') }}" autocomplete="off" id="UserRegisterForm" method="post" accept-charset="utf-8">
                  @csrf
                  <div class="form-group text-left required">
                      <label for="name" class="form-label"><b>Name</b></label>
                      <input type="text" name="name" id="name" class="form-control @error('name') is-invalid @enderror" placeholder="Enter your Name" required="required"
                      >
                      @error('name')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                  </div>
                  <div class="form-group text-left required">
                     <label for="username" class="form-label"><b>Username</b></label>
                     <input type="text" name="username" id="username" class="form-control form-input @error('username') is-invalid @enderror" placeholder="Username" required="required">
                     @error('username')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                 </div>
                 <div class="form-group text-left">
                     <label for="email" class="form-label"><b>Email</b></label>
                     <input type="email" name="email" id="email" class="form-control form-input @error('email') is-invalid @enderror" placeholder="Email Address" required="required">
                     @error('email')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                 </div>
                 <div class="form-group text-left">
                     <label for="password" class="form-label text-left"><b>Password</b></label>
                    <input type="password" name="password" id="password" class="form-control form-input @error('password') is-invalid @enderror" placeholder="Password" required="required">
                    <div class="form-hint">Password must be at least 8 characters long.</div>
                    @error('password')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                </div>
                <div class="form-group text-left required">
                    <label for="password_confirmation" class="form-label text-left"><b>Confirm Password</b></label>
                   <input type="password" name="password_confirmation" id="password_confirmation" class="form-control form-input  @error('password_confirmation') is-invalid @enderror" placeholder="Confirm Password" required="required">
                   @error('password_confirmation')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
               </div>
                <div class="form-group">
                    <label for="UserPrivacy" class="splash-check">
                        <input type="hidden" name="data[User][privacy]" id="UserPrivacy_" value="0"/>
                        <input type="checkbox" name="data[User][privacy]"  required="required" value="1" id="UserPrivacy" onClick="EnableSubmit(this)" />
                        <span class="checkb">&nbsp;</span>
                        <strong>I’ve read and understood the <a href="/terms-conditions" target="_blank" title="Terms of Service">Terms of Service</a></strong>
                    </label>
                </div>
                <div class="form-group">
                    <label for="Newsletter" class="splash-check">
                        <input type="hidden" name="newsletter" id="Newsletter_" value="0"/>
                        <input type="checkbox" name="newsletter"  value="1" id="Newsletter" required="required"/>
                        <span class="checkb">&nbsp;</span><strong>I would like to receive occasional e-mails with discounts and offers</strong>
                    </label>
                </div>
                <div class="form-group">
                        <div class="row">
                            <div class="col-sm-6 text-left left-col-panel">
                                <p>Already Have an Account? <a href="/login" class="text-panel-link">Login</a></p>
                            </div>
                            <div class="col-sm-6 text-right">
                                <button type="submit" class="btn btn-login">Sign Up</button>
                            </div>
                        </div>
                </div>
              </form>
tykus's avatar
tykus
Best Answer
Level 104

Why is member_id a required input in the validation rules? It is not in the Request, you calculate it in the create method; comment (or remove) that Rule:

    protected function validator(array $data)
    {

        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'username' => ['required ', 'string' ,'max:255' ,'unique:users'],
            // 'package' => ['required'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
           // 'member_id' => ['required', 'string', 'max:255'],
        ]);
    }

Please or to participate in this conversation.