Add slug to user (RainLab) OctoberCms

958 Views Asked by At

Is it possible to add a slug in the user table, if it's possible, then how do?

I try to do it as usual, but it does not work with the table user

class User extends Model{
use \October\Rain\Database\Traits\Sluggable;

protected $slugs = ['slug' => 'name'];

}

I add the slug field in the user table, but it's still null

thank you in advance

1

There are 1 best solutions below

0
On

It will be little lengthy but No problem We will Solve it :)

There are 2 ways of doing it.

  1. Add direct field to user table and start using it and we are done.
  2. Create other table UserExtension in database [ obvious with new plugin ] and add dynamic relation ship oneToOne to user table to this table and then save all new data to this table using relation.

For now we should go for 1st as seems you want only one field so

  1. add actual field in to table [ database table ]
  2. extend backend forms to show that field [saving data will work automatically]

Create update script [ Class name => AddSlugToUserTable ] file name will be in snake case => add_slug_to_user_table.php. Add this file to your plugin's updates directory.

<?php namespace HardikSatasiya\Plugin\Updates;

use Schema;
use October\Rain\Database\Updates\Migration;

class AddSlugToUserTable extends Migration
{
    public function up()
    {
        Schema::table('users', function($table)
        {
            // make it nullable as we are adding it and existing records may not have any data for it so
            $table->string('slug')->nullable()->index();
        });

       // or even you can add converted data to slug field
       // from existing user name
    }

    public function down()
    {
        // don't want to mess with data so better be empty
    }
}

Now add details about this file in version.yaml file. create this file if its not inside updates folder. [ this file is very sensitive about spacing so use 2 space for tab and avoid extra spaces. ]

1.0.1:
    - Initialize plugin.
1.0.2:
    - Adding Slug Field to User Table.
    - add_slug_to_user_table.php

Next step will be adding Form Field to the Backend Form add this code to your plugin.php => boot method.

class Plugin extends PluginBase
{
    [...]

    public function boot()
    {
        // Extend all backend form usage
        \Event::listen('backend.form.extendFields', function($widget) {

            // Only for the User controller
            if (!$widget->getController() instanceof \RainLab\User\Controllers\Users) {
                return;
            }

            // Only for the User model
            if (!$widget->model instanceof \RainLab\User\Models\User) {
                return;
            }

            // Add an extra birthday field
            $widget->addFields([
                'slug' => [
                    'label'   => 'Slug',
                    'comment' => 'Add Slug To User',
                    'preset'    => [
                         'field' => 'name',
                         'type' => 'slug'
                     ]
                ]
            ]);

        });
    }
}

Now Logout [ if you are already logged In ] from backend and Login again, to take effect all this stuff. Then Open User From.

enter image description here

You will see your shiny new slug field which can automatically filled from the name [ you can change it from preset config ]

if you get any problem or doubts please comment.