-
Notifications
You must be signed in to change notification settings - Fork 27
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
Comments
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
Read more about that in this Link
<?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);
}
}
use App\Services\ReplaceDotFromLogChangesPipe;
...
public function boot(): void
{
Order::addLogChange(new ReplaceDotFromLogChangesPipe()); // assuming "Order" is your model here,
}
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 Thank You |
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. |
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:
The text was updated successfully, but these errors were encountered: