Laravel-admin: what is the best way to add a new field to administrator setting form?

Created on 7 Feb 2018  ·  6Comments  ·  Source: z-song/laravel-admin

I'd like to add a locale field to administrator setting form, what is the best way to do this please?

Most helpful comment

I had a solution
1- Create new Model extend from Administrator and add new field locale

class CustomAdministrator extends Administrator {
    public function __construct( array $attributes = [] ) {
        parent::__construct( $attributes );
        array_push( $this->fillable, 'locale' );
    }
}

2- Create a new Controller extend from UserController

class CustomUserController extends UserController {
    protected function grid() {
        $g = parent::grid();
        $g->column( 'locale', 'Locale' );
        return $g;
    }
    public function form() {
        $f = parent::form();
        $f->text( 'locale' );
        return $f;
    }
}

3- create a new route:

$router->resource( 'test', CustomUserController::class );

4- Call URL on browser:

localhost:8000/admin/test

All 6 comments

I met the same issue, please help

@amun1303, fork laravel-admin, replace its route with yours, you might need to extends its controller, or completely use your own. Laravel-admin doesn't have a power form like Symfony Form by which you can customize all other forms without touching its code. I will close this , because there is no easy way to do the job like Symfony in Laravel system.

@videni See https://github.com/z-song/laravel-admin/issues/1739#issuecomment-383784417

Do not need to fork and modify the code, just override the route and controller

I had a solution
1- Create new Model extend from Administrator and add new field locale

class CustomAdministrator extends Administrator {
    public function __construct( array $attributes = [] ) {
        parent::__construct( $attributes );
        array_push( $this->fillable, 'locale' );
    }
}

2- Create a new Controller extend from UserController

class CustomUserController extends UserController {
    protected function grid() {
        $g = parent::grid();
        $g->column( 'locale', 'Locale' );
        return $g;
    }
    public function form() {
        $f = parent::form();
        $f->text( 'locale' );
        return $f;
    }
}

3- create a new route:

$router->resource( 'test', CustomUserController::class );

4- Call URL on browser:

localhost:8000/admin/test

@amun1303 @z-song thanks a lot.

I had a solution
1- Create new Model extend from Administrator and add new field locale

class CustomAdministrator extends Administrator {
  public function __construct( array $attributes = [] ) {
      parent::__construct( $attributes );
      array_push( $this->fillable, 'locale' );
  }
}

2- Create a new Controller extend from UserController

class CustomUserController extends UserController {
  protected function grid() {
      $g = parent::grid();
      $g->column( 'locale', 'Locale' );
      return $g;
  }
  public function form() {
      $f = parent::form();
      $f->text( 'locale' );
      return $f;
  }
}

3- create a new route:

$router->resource( 'test', CustomUserController::class );

4- Call URL on browser:

localhost:8000/admin/test

Worked for me after I inversed lines inside constructor :

        public function __construct( array $attributes = [] ) {
        array_push( $this->fillable, 'locale' );
        parent::__construct( $attributes );
    }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

xiaalngf picture xiaalngf  ·  3Comments

cdhraesaemer picture cdhraesaemer  ·  3Comments

taimaiduc picture taimaiduc  ·  3Comments

MarKco picture MarKco  ·  3Comments

amun1303 picture amun1303  ·  3Comments