Adding additional user data

User Module

The User module is nice for handling authentication and registration, but it is very likely that you are going to want to add more data to your user then the default first and last name. Here we're going to see how to add more data to our user.

Creating a Profile module

First thing we need to do is creating a Profile module. You can do this using the Module Scaffold tool, or by creating the module manually.

For this purpose of this guide, we'll be using the module scaffold command. Start by running php artisan asgard:module:scaffold in your terminal, enter your-vendor-name/profile as the module name. Next select Eloquent, followed by entering the Profile entity. Finally leave the rest empty to finalise the scaffolding.

Don't forget to add the module in the Modules/.gitignore file by adding !Profile in that file.

By default the scaffolder tool will create entities as if they were translatable, we don't need this feature on the Profile so we can remove the ProfileTranslation entity and the migrations for the translations.

Adding custom profile fields

Open the migration file that was generated, and add the fields you would like to have on the use profile. As an example:

Schema::create('profile__profiles', function (Blueprint $table) {


Now run the migrations using php artisan module:migrate Profile.

Publishing user views

Next, we're going to add those fields on on register page. Since the register views are in the User module, which we cannot edit (consider any module not in the .gitignore as a package in the vendor folder). To customize the login and register views we're going to publish those views using the following command:

php artisan vendor:publish --provider="Modules\User\Providers\UserServiceProvider"

After this you'll see the views have been published in the resources/views/asgard folder.

Adding custom fields on the register view

Now that we have the register view, open it and add the fields by following the following field naming convention :


An example from our migration :

<div class="form-group{{ $errors->has('') ? ' has-error has-feedback' : '' }}">
    {!! Form::label('profile[tel]', trans('')) !!}
    <input class="form-control" placeholder="{{ trans('') }}"
           name="profile[tel]" type="text" id="profile[tel]" value="{{ Input::old('') }}">
    {!! $errors->first('', '<span class="help-block">:message</span>') !!}

<div class="form-group{{ $errors->has('profile.street') ? ' has-error has-feedback' : '' }}">
    {!! Form::label('profile[street]', trans('profile::profiles.street')) !!}
    <input class="form-control" placeholder="{{ trans('profile::profiles.street') }}"
           name="profile[street]" type="text" id="profile[street]" value="{{ Input::old('profile.street') }}">
    {!! $errors->first('profile.street', '<span class="help-block">:message</span>') !!}

<div class="form-group{{ $errors->has('profile.number') ? ' has-error has-feedback' : '' }}">
    {!! Form::label('profile[number]', trans('profile::profiles.number')) !!}
    <input class="form-control" placeholder="{{ trans('profile::profiles.number') }}"
           name="profile[number]" type="text" id="profile[number]" value="{{ Input::old('profile.number') }}">
    {!! $errors->first('profile.number', '<span class="help-block">:message</span>') !!}

 <div class="form-group{{ $errors->has('profile.additional') ? ' has-error has-feedback' : '' }}">
    {!! Form::label('profile[additional]', trans('profile::profiles.additional')) !!}
    <input class="form-control" placeholder="{{ trans('profile::profiles.additional') }}"
           name="profile[additional]" type="text" id="profile[additional]" value="{{ Input::old('profile.additional') }}">
    {!! $errors->first('profile.additional', '<span class="help-block">:message</span>') !!}

<div class="form-group{{ $errors->has('') ? ' has-error has-feedback' : '' }}">
    {!! Form::label('profile[zip]', trans('')) !!}
    <input class="form-control" placeholder="{{ trans('') }}"
           name="profile[zip]" type="text" id="profile[zip]" value="{{ Input::old('') }}">
    {!! $errors->first('', '<span class="help-block">:message</span>') !!}

<div class="form-group{{ $errors->has('') ? ' has-error has-feedback' : '' }}">
    {!! Form::label('profile[place]', trans('')) !!}
    <input class="form-control" placeholder="{{ trans('') }}"
           name="profile[place]" type="text" id="profile[place]" value="{{ Input::old('') }}">
    {!! $errors->first('', '<span class="help-block">:message</span>') !!}

And that's all there is to it to add our custom fields on the view.

Setting up relations

The final thing we need to do is setting up the relation, both on the Profile entity as on the User entity. A profile belongs to a user and a user belongs to a profile.

On Profile entity

This one is easy, though we need to take into account AsgardCms can have multiple user drivers, so our user() method on the Profile will look like this:

public function user()
    $driver = config('asgard.user.config.driver');

    return $this->belongsTo("Modules\\User\\Entities\\{$driver}\\User");

This is added on the Profile entity found at Modules/Profile/Entities/Profile.php.

We're reading the user driver from the configuration and using that to setup our relation.

Now to get the user from a profile just use $profile->user->id.

On User entity

This one is special since we cannot edit our User module directly, since this module is managed by composer and is not present in our Modules/.gitignore file.

To add the relation on our User, we need to edit the config.php file in the /config/asgard/user/ folder of our application.

Navigate to the section titled Dynamic relations, by default it looks like this:

     | Dynamic relations
     | Add relations that will be dynamically added to the User entity
    'relations' => [
//        'extension' => function ($self) {
//            return $self->belongsTo(UserExtension::class, 'user_id', 'id')->first();
//        }

You can add a relation for the profile like so:

    'relations' => [
        'profile' => function ($self) {
            return $self->hasOne(\Modules\Profile\Entities\Profile::class, 'user_id', 'id');

We can now access the user profile information using $user->profile->street.

Adding data on the users table

You can add additional columns on the users table if that's really needed. For instance if you want to have an username.

In the users configuration file, located at config/asgard.user.users.php, there's a fillable key which contains an array of fillable fields for the user object.

Add the fields you want in this array.

Mobile Analytics