Ability to display foreign ids as a field from the related table #32

Open
opened 2023-12-14 12:38:42 +00:00 by ShaunWilders · 2 comments
ShaunWilders commented 2023-12-14 12:38:42 +00:00 (Migrated from github.com)

Foreign IDs are display as an ID. Ideally we'd be able to specify how these fields are displayed using data from the related table:

image

Foreign IDs are display as an ID. Ideally we'd be able to specify how these fields are displayed using data from the related table: ![image](https://github.com/pxlrbt/filament-activity-log/assets/30116257/747e7fa2-12c9-461a-83d1-49e3b54199af)
iprastha commented 2024-02-14 13:43:33 +00:00 (Migrated from github.com)

Hi, i was having the same issue with you, especially displaying BelongsTo relationship. After reading in spatie/activitty-log package, there is a way to solve this using dot notation, for example

->logOnly(['interpreter.name']

Read more about that in this Link
However, using the dot notation causes issue with this package, because this package uses the get_data helper where it will break array structure using the dot notation as well.
Therefore currently I am implementing a custom pipe in spatie activity log using LoggablePipe (read more about it here) to replace all dot notation in the changes with underscore instead. This is what I did:

  1. In your laravel project , under App\Services , create the file ReplaceDotFromLogChangesPipe.php
<?php
namespace App\Services;

use Closure;
use Illuminate\Support\Arr;
use Spatie\Activitylog\EventLogBag;
use Spatie\Activitylog\Contracts\LoggablePipe;

class ReplaceDotFromLogChangesPipe implements LoggablePipe
{

    public function handle(EventLogBag $event, Closure $next): EventLogBag
    {
        $event->changes['attributes'] = Arr::mapWithKeys($event->changes['attributes'], function (string $value, string $key){
            return [str_replace('.','_',$key)=>$value];
        });
        $event->changes['old'] = Arr::mapWithKeys($event->changes['old'], function (string $value, string $key){
            return [str_replace('.','_',$key)=>$value];
        });

        return $next($event);
    }
}
  1. In your laravel project , under App\Providers\AppServiceProvider.php , add the following:
use App\Services\ReplaceDotFromLogChangesPipe;
...
public function boot(): void
    {
Order::addLogChange(new ReplaceDotFromLogChangesPipe()); // assuming "Order" is your model here, 
}
  1. In your model under getActivitylogOptions(), use the dot notation as mentioned in spatie documentation
public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->logOnly(['customer.name']); // using the dot notation same as in the relattionship defined in this model
    }

Now you can log any relationship using dot notation without breaking this package. I hope later on the owner of this package can see this solution and comeup with a more permanent solution built into the next version
Because using the dot notation is a standard method in the spatie package which this package relies on
Tagging @pxlrbt

Thank You

Hi, i was having the same issue with you, especially displaying BelongsTo relationship. After reading in spatie/activitty-log package, there is a way to solve this using dot notation, for example ``` ->logOnly(['interpreter.name'] ``` Read more about that in this [Link](https://spatie.be/docs/laravel-activitylog/v4/advanced-usage/logging-model-events#content-logging-directly-related-model-attributes) However, using the dot notation causes issue with this package, because this package uses the get_data helper where it will break array structure using the dot notation as well. Therefore currently I am implementing a custom pipe in spatie activity log using LoggablePipe (read more about it [here](https://spatie.be/docs/laravel-activitylog/v4/advanced-usage/manipulate-changes-array)) to replace all dot notation in the changes with underscore instead. This is what I did: 1. In your laravel project , under App\Services , create the file ReplaceDotFromLogChangesPipe.php ```php <?php namespace App\Services; use Closure; use Illuminate\Support\Arr; use Spatie\Activitylog\EventLogBag; use Spatie\Activitylog\Contracts\LoggablePipe; class ReplaceDotFromLogChangesPipe implements LoggablePipe { public function handle(EventLogBag $event, Closure $next): EventLogBag { $event->changes['attributes'] = Arr::mapWithKeys($event->changes['attributes'], function (string $value, string $key){ return [str_replace('.','_',$key)=>$value]; }); $event->changes['old'] = Arr::mapWithKeys($event->changes['old'], function (string $value, string $key){ return [str_replace('.','_',$key)=>$value]; }); return $next($event); } } ``` 2. In your laravel project , under App\Providers\AppServiceProvider.php , add the following: ```php use App\Services\ReplaceDotFromLogChangesPipe; ... public function boot(): void { Order::addLogChange(new ReplaceDotFromLogChangesPipe()); // assuming "Order" is your model here, } ``` 3. In your model under getActivitylogOptions(), use the dot notation as mentioned in spatie documentation ```php public function getActivitylogOptions(): LogOptions { return LogOptions::defaults() ->logOnly(['customer.name']); // using the dot notation same as in the relattionship defined in this model } ``` Now you can log any relationship using dot notation without breaking this package. I hope later on the owner of this package can see this solution and comeup with a more permanent solution built into the next version Because using the dot notation is a standard method in the spatie package which this package relies on Tagging @pxlrbt Thank You
ShaunWilders commented 2024-04-04 14:24:14 +00:00 (Migrated from github.com)

Works well, thank you. The only place where it doesn't work is on belongsToMany methods but looks like this package doesn't store changes to these at all anyway so I will need to have a think about how to log those kind of changes.

Works well, thank you. The only place where it doesn't work is on belongsToMany methods but looks like this package doesn't store changes to these at all anyway so I will need to have a think about how to log those kind of changes.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
pxlrbt/filament-activity-log#32
No description provided.