0

I'm using the laravel-users package to add user management functionality to my Laravel 5.7 application but I'm struggling to figure out how to add additional fields to the user create / user edit forms so that they match my user table schema.

Users table definition:

    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->string('phone_number', 13);
        $table->boolean('is_super_admin')->default(false);
        $table->rememberToken();
        $table->timestamps();
    });

Specifically I want to add the phone_number field to the forms. I've updated the blade template adding in the markup for the field:

                <div class="form-group has-feedback row {{ $errors->has('phone_number') ? ' has-error ' : '' }}">
                    @if(config('laravelusers.fontAwesomeEnabled'))
                        {!! Form::label('phone_number', __('auth.phone_number'), array('class' => 'col-md-3 control-label')); !!}
                    @endif
                    <div class="col-md-9">
                        <div class="input-group">
                            {!! Form::text('phone_number', NULL, array('id' => 'phone_number', 'class' => 'form-control', 'placeholder' => __('auth.phone_number'))) !!}
                            <div class="input-group-append">
                                <label class="input-group-text" for="phone_number">
                                    @if(config('laravelusers.fontAwesomeEnabled'))
                                        <i class="fa fa-fw {!! __('laravelusers::forms.create_user_icon_username') !!}" aria-hidden="true"></i>
                                    @else
                                        {!! __('auth.phone_number') !!}
                                    @endif
                                </label>
                            </div>
                        </div>
                        @if ($errors->has('phone_number'))
                            <span class="help-block">
                                    <strong>{{ $errors->first('phone_number') }}</strong>
                                </span>
                        @endif
                    </div>
                </div>

But it seems that the actual creation of the user is handled by the UsersManagementController that's part of the package itself. How can I override this so that I can have it store the new field I've added?

PiX06
  • 431
  • 4
  • 17

1 Answers1

0

Without reading in detail the full explanation (since I have currently little reputation on the present website to just post a few comments to clarify the question, rather than an attempt an answer...) here is some advice ;)

You probably need to go into the UsersManagementController and edit the update function to allow to save the newly added field "phone_number".

Most likely your controller would be located in:

app/Http/Controllers/UsersManagementController.php

the function you need to edit will look something like this

public function update(Request $request, $id) {

// Find the User that needs to be updated
$user = Post::find($id);
$user->phone_number = $request->input('phone_number');
//... some more code here
$user->save();

return //whatever is returned here... potentially redirected
}

To find all list of routes, in case this is the issue and consecutively adapt your form post to submit the data to the correct controller (this will list all you currently managed routes):
php artisan route:list

If there is no controller you should create one and make sure to add it into the:

routes/web.php

Hope this helps

dankilev
  • 720
  • 2
  • 10
  • 32
  • Thanks for your answer. It seems that rather than generating a controller in the application code at app/Http/Controllers it uses a controller in the /vendor/ directory which obviously can't be edited - this is why I was stuck – PiX06 Feb 24 '19 at 15:08
  • You are welcome. In this case you may want to create a new controller that will control a new route for the update of the required field and send the form data to the new route. The new controller can be made to extend the original one but this would depend on your cooding preferences and how you plan to use the new controller. – dankilev Feb 24 '19 at 16:40