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.
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);
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,
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.
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 :
don't forget to run 'composer dumpautoload'
change in app\config.php the database.model.users_model setting in admin.php to the following :
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 !