Laravel-admin: Add relation to admin user model

Created on 5 Mar 2019  ·  4Comments  ·  Source: z-song/laravel-admin

  • Laravel Version: 5.7.26
  • PHP Version: 7.1.9
  • Laravel-admin: 1.6.9

Description:

I want to add a foreign key (shop_id) to admin_users table in order to filter grid model based on each admin user's shop.
Every user with shop_manager role sholud access their own products not all of the products.

Steps To Reproduce:

I want to be able to use something like this:
if(Admin::user()->isRole('shop_manager'))
$grid->model()->where('shop_id', '=', Admin::user()->shop_id);

wontfix

Most helpful comment

It's possible to override the model of admin_users table.
Please following this :

  • copy the file vendor\encore\laravel-adminsrc\AuthDatabase\Administrator.php to for example app\Admin\Models\Administrator.php

  • add a composer setting to map your new model folder like this :

        "psr-4": {

            "App\\": "app/",

            "MyProject\\Models\\": "app/Admin/Models/"

        }

  • don't forget to run 'composer dumpautoload'

  • change in app\config.php the database.model.users_model setting in admin.php to the following :

 // User tables and model.

'users_table' => 'admin_users',

'users_model' => MyProject\Models\Administrator::class,
  • last thing add this use to your new Administrator.php class :

use Encore\Admin\Auth\Database\HasPermissions;

Now laravel-admin uses you Administrator class for the admin_users table.

So you can add relationships you want or columns you need.

Hope it helps !

All 4 comments

I have very close need. Globally is there a possibility to extends or override some of admin models here ?

Just discover @z-song already reply to this topic elsewhere : https://github.com/z-song/laravel-admin/issues/1739#issuecomment-383784417

Thanks to him. :-)

It's possible to override the model of admin_users table.
Please following this :

  • copy the file vendor\encore\laravel-adminsrc\AuthDatabase\Administrator.php to for example app\Admin\Models\Administrator.php

  • add a composer setting to map your new model folder like this :

        "psr-4": {

            "App\\": "app/",

            "MyProject\\Models\\": "app/Admin/Models/"

        }

  • don't forget to run 'composer dumpautoload'

  • change in app\config.php the database.model.users_model setting in admin.php to the following :

 // User tables and model.

'users_table' => 'admin_users',

'users_model' => MyProject\Models\Administrator::class,
  • last thing add this use to your new Administrator.php class :

use Encore\Admin\Auth\Database\HasPermissions;

Now laravel-admin uses you Administrator class for the admin_users table.

So you can add relationships you want or columns you need.

Hope it helps !

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

joernroeder picture joernroeder  ·  3Comments

amun1303 picture amun1303  ·  3Comments

chenyongmin picture chenyongmin  ·  3Comments

piian picture piian  ·  3Comments

abufalbo picture abufalbo  ·  3Comments