Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ShaunWilders opened this issue Dec 14, 2023 · 2 comments
Open

Comments

@ShaunWilders
Copy link

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
Copy link

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

@ShaunWilders
Copy link
Author

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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants