platform icon indicating copy to clipboard operation
platform copied to clipboard

Admin command do not use app User model, but internal Orchid User model

Open ppalmeida opened this issue 3 years ago • 1 comments

Describe the bug I start this (maybe a) bug issue with a question: should Orchid Admin command use which User model? Because AdminCommand file uses Orchid User model. And not the regular App\Models\User default model in my application.

And why this is a problem? My Model currently uses EfficientUUID traits. The trait generates UUID when creating new Users. Since that trait is not present in Orchid internal User model, the SQL fails for not providing one:

 What is your name? [admin]:
 >

 What is your email? [[email protected]]:
 >

 What is the password?:
 >

SQLSTATE[HY000]: General error: 1364 Field 'uuid' doesn't have a default value (SQL: insert into `users` (`name`, `email`, `password`, `permissions`, `updated_at`, `created_at`) values (admin, [email protected], $2y$10$zXR3B5cnNX9DyFgrxjkMiOOEsx5XZHjbhIvPihFmPi6N0DFTtAwES, {"platform.systems.roles":true,"platform.systems.users":true,"platform.systems.attachment":true,"platform.index":true}, 2022-06-04 11:25:03, 2022-06-04 11:25:03))

To Reproduce Steps to reproduce the behavior:

  1. Create a default Laravel application
  2. Install Orchid
  3. Add any other library or trait that needs or creates a DB column related to the User model in app/Models/User.php file.
  4. Try create an admin using Orchid artisan command

Expected behavior The admin user is created as expected

Additional context Maybe it could be a good idea to be able inform the User class to be used in the platform.php config file? Because, doing that could be easier to add traits or changes to the User class and keep Orchid working as expected. Of course, considering that the User class extends Orchid\Platform\Models\User class.

ppalmeida avatar Jun 04 '22 14:06 ppalmeida

Hi @ppalmeida The model and database may vary depending on the project. Also, the package can be installed into an existing application, for this reason we use the pre-installed model, but we give the opportunity to change this behavior:

Dashboard::useModel(\Orchid\Platform\Models\User::class, \App\User::class);

More details can be found in the documentation at the following link: https://orchid.software/en/docs/configuration/#model-classes

After that, you can also override the used method in your own model:

public static function createAdmin(string $name, string $email, string $password)
{
    //... you code
}

tabuna avatar Jun 04 '22 19:06 tabuna