pxlrbt / filament-activity-log

Spatie activity log integration into Filament
MIT License
144 stars 25 forks source link

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

Open ShaunWilders opened 9 months ago

ShaunWilders commented 9 months ago

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

iprastha commented 7 months ago

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);
}

}


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, 
}
  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

ShaunWilders commented 6 months ago

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.