Laravel 5: How to determine if the function Model :: create () is called from a craftsman?

I'm working on a project Laravel 5, which requires that each model had tracking user attributes ( created_by, updated_by, deleted_byetc.).

I can easily identify the current user with Auth::guest()and Auth::user().

I want these attributes to be the default for a particular user (e.g. sysadmin) if called from artisan tinker.

The problem is twofold:

  • Auth::guest() cannot distinguish between master-masters and actual guest in a web application.
  • I do not have to call directly MyModel::create()to be able to transmit special instructions. It can be called from another class or namespace.

The only solutions I can think of are as follows:

  • Somehow determine if a call is made from artisan tinkeror
  • Be able or require a user to log in artisan tinker

Is it possible?

+4
source share
1 answer

Artisan Tinker is an individual version of Psysh . It can run any PHP code. So, two solutions:

  • Before testing with Tinker, run #define IS_TINKERand verify this constant in your logic. This is a bit hacky, and it would be better if Tinker / Psysh sets the flag if it is running, however I could not find such a mechanism.

  • Log in to the appropriate user accounts inside Tinker

0
source

Source: https://habr.com/ru/post/1607192/


All Articles