dev-partners / laravel-nova-auditable-log

A Laravel Nova resource tool for visualizing the audit log created by the Laravel Auditing package.
51 stars 22 forks source link

How to display the user name in Audit Log UI if there is no name field? #11

Closed ludgerey closed 3 years ago

ludgerey commented 4 years ago

The user is not shown in the Audit Log UI. I assume it's because I don't have the default name field on the user. I am using given_name and family_name. Is there a way to render the user name in that audit log?

Bildschirmfoto 2020-01-29 um 17 08 08

The corresponding HTTP response:

{
  "status": "OK",
  "audits": {
    "current_page": 1,
    "data": [
      {
        "id": 664,
        "user_type": "App\\User",
        "user_id": 2,
        "event": "updated",
        "auditable_type": "App\\User",
        "auditable_id": 2,
        "old_values": {
          ...
        },
        "new_values": {
          ...
        },
        "url": ...,
        "ip_address": "127.0.0.1",
        "user_agent": "Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/79.0.3945.130 Safari\/537.36",
        "tags": null,
        "created_at": "2020-01-29 16:07:07",
        "updated_at": "2020-01-29 16:07:07",
        "user": {
          "id": 2,
          "given_name": "John",
          "family_name": "Doe",
          "email": "test@example.com",
          "email_verified_at": "2020-01-29 14:54:55",
          "created_at": "2020-01-29 14:54:55",
          "updated_at": "2020-01-29 16:07:07",
          "deleted_at": null
        }
      },
      ...
    ],
  },
}

I think the perfect solution would be a rendering like Nova does in the usual data grids: Using the Nova Resource for title() and a link to the corresponding record.

recycledbeans commented 4 years ago

Hey @ludgerey I think the idea of using the title is a good idea and would be open to adding that feature at some point. In the meantime, if you do not use the name attribute in your User model, you can create a dynamic name attribute using a model accessor ( See defining an accessor: https://laravel.com/docs/6.x/eloquent-mutators#defining-an-accessor).

In addition to this, I would recommend adding that attribute to the appends array of your User model as well, so that it is added to the JSON output (See appending values to JSON: https://laravel.com/docs/6.x/eloquent-serialization#appending-values-to-json).

I hope this helps!

stale[bot] commented 4 years ago

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.

recycledbeans commented 3 years ago

I'm going actually close this issue since the name field is pretty common and the accessor method should work for the majority of cases that don't use a "name" field.