I want to add few more fields in my backend administrator by creating my own plugin called as Users. Here is what I have done so far to create few new fields.
plugins\technobrave\users\Plugin.php
<?php namespace Technobrave\Users;
use System\Classes\PluginBase;
use Backend\Models\User as BackendUserModel;
use Backend\Controllers\Users as BackendUsersController;
class Plugin extends PluginBase
{
public function registerComponents()
{
}
public function registerSettings()
{
}
public function boot()
{
// Add college and teacher field to Users table
BackendUserModel::extend(function($model){
$model->belongsTo['team'] = ['technobrave\team\Models\Team'];
});
// Add college and teacher field to Users form
BackendUsersController::extendFormFields(function($form, $model, $context){
if (!$model instanceof BackendUserModel)
return;
$form->addTabFields([
'team' => [
'label' => 'Team',
'comment' => 'Associate this user with a team.',
'type' => 'recordfinder',
'list' => '$/technobrave/team/models/team/columns.yaml',
'prompt' => 'Click the %s to find a team',
'select' => 'id',
'nameFrom'=> 'name',
'tab' => 'Account'
]
]);
});
}
}
plugins\technobrave\users\models\User.php
<?php namespace Technobrave\Users\Models;
use Model;
/**
* Model
*/
class User extends Model
{
use \October\Rain\Database\Traits\Validation;
/*
* Validation
*/
public $rules = [
'team_id' => 'required',
];
public $customMessages = [
'team_id.required' => 'Please select Team',
];
/**
* @var string The database table used by the model.
*/
public $table = 'backend_users';
}
And now this is how my Administrator page looks like.
And if I click on "Team Recordfinder" I am able to see like below.
And if I select any of the team record from the list, its looks like below.
As you can see all working fine so far. But as soon as I fill full form, I am getting this SQL error saying column not found. Like below.
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'team_id' in 'field list' (SQL: insert into
backend_users
(is_superuser
,login
,first_name
,last_name
,password
,persist_code
,permissions
,team_id
,updated_at
,created_at
) values (0, Johny, johny@mailinator.com, Johny, Cook, y$bbr00J5O2dE1WDHHWZYHIeMduXI82HkDnE8IYBcAet4ie0nfpgpwq, , , 9, 2017-05-19 06:23:49, 2017-05-19 06:23:49))" on line 666 of C:\xampp\htdocs\slp_website_cms\vendor\laravel\framework\src\Illuminate\Database\Connection.php
Now, My question is, do I need to add team_id
field manually in backend_users table ? Is it valid way ? I am not sure.
Additionally, I want to make validations for this particular field hence I did below.
plugins\technobrave\users\models\User.php
<?php namespace Technobrave\Users\Models;
use Model;
/**
* Model
*/
class User extends Model
{
use \October\Rain\Database\Traits\Validation;
/*
* Validation
*/
public $rules = [
'team_id' => 'required',
];
public $customMessages = [
'team_id.required' => 'Please select Team',
];
/**
* @var string The database table used by the model.
*/
public $table = 'backend_users';
}
But validations are not working either. How can I validate this field if some one checked "Property Consultant" from User Role ?
Lot of questions but I need a best way to approach towards this issue. Can someone guide me to make this thing work ?
Thanks
do I need to add team_id field manually in backend_users table ? Is it valid way ? I am not sure.
Yes, you need to create a migration to add this field to the table.
For adding validation rules you also can extend model:
public function boot() {
BackendUserModel::extend(function($model){
$myrules = $model->rules;
$myrules['team_id'] = 'required';
$model->rules = $myrules;
});
}
For getting logged in user you can use App::before
public function boot() {
\App::before(function() {
// Here BackendAuth::getUser() is already initialized
)};
}