-
-
Notifications
You must be signed in to change notification settings - Fork 718
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
Return log with causer and subject nested relations #281
Comments
Hi @saqueib Use (I think) another option would be to use |
I'm having this exact same issue at the moment. When attempting to eager load for models that don't have the relationship, it causes an error. @saqueib, have you been able to find a solution to this yet? |
Is there anyway to get the associated model name from the attributes?
It does not make sense to show on the UI: changed from ID to ID Any thoughts? |
|
@Gummibeer Sorry, I think maybe I did not make myself clear enough.
This could be changed to
|
@skcin7 just check if subject is @AlexVanderbist even if the |
@fernandocoronatomf add a fillable attribute with nested relation, something like this // relation to get the status text from model
$logAttributes = ['projectStatus.name'];
public function projectStatus()
{
return $this->belongsTo(ProjectStatus::class);
} then you will be able to get the |
@saqueib I can see what you are saying, but that is not very helpful or just help me out understanding what you meant... If I have the project_status_id in my properties new values json, how exactly getting it from $subject->projectStatus->name would help? I would like to loop through the json properties and just print them on my VUE component |
I've found two work arounds to this. Solution OneFirstly you could modify laravel-activitylog/src/ActivityLogger.php Lines 198 to 201 in 492fa4c
And utilise array_reduce() instead: $nestedProperty = explode('.', $propertyName);
return array_reduce($nestedProperty, function($attributeValue, $propertyName) {
return $attributeValue->$propertyName;
}, $attributeValue);
//$attributeValue = $attributeValue->toArray();
//return array_get($attributeValue, $propertyName, $match); Note you shouldn't just copy and paste this as it will need some additional checks to see if the property exists and is an object, but you get the idea. Solution TwoThe second solution is to use a regular expression to match all the relationships and preload them. Simply append the following to the top of if (preg_match_all('/:([a-z0-9._-]+)\.[a-z0-9_-]+/i', $description, $matches)) {
$activity->load($matches[1]);
} What this will do is match all the properties minus the final attribute. So in the case of:
It will match the following:
And then load those relationships on the model. Then when Would a PR for one of these be accepted? |
It's actually incredibly easy to overwride the activitylogger class and implement this yourself. Simply create a new class: <?php
namespace App;
use Spatie\Activitylog\ActivityLogger;
use Spatie\Activitylog\Contracts\Activity;
class CustomActivityLogger extends ActivityLogger
{
/**
* @param string $description
* @param \Spatie\Activitylog\Contracts\Activity $activity
* @return string
*/
protected function replacePlaceholders(string $description, Activity $activity): string
{
if (preg_match_all('/:([a-z0-9._-]+)\.[a-z0-9_-]+/i', $description, $matches)) {
$activity->load($matches[1]);
}
return parent::replacePlaceholders($description, $activity);
}
} Then in $this->app->bind(\Spatie\Activitylog\ActivityLogger::class, \App\CustomActivityLogger::class); And tada it works! |
@robjbrain I would change the @fernandocoronatomf after a long time we get a new feature that could help you #472 . |
Hey @Gummibeer I didn't have array_get() in my code that is what's in the current spatie implementation, you're right though that works fine. Perhaps the main package should be updated to include it? Example: return data_get($attributeValue, $propertyName, $match);
//$attributeValue = $attributeValue->toArray();
//return array_get($attributeValue, $propertyName, $match); |
@robjbrain that was my idea behind 😉 |
Is there any way I can load nested relation for causer and subject.
For example, I have
Post
andAttachment
This works but I also have other
Topics
model which doesn't have this attachments relation so it blowsHow I can solve this, I am login many other models as well.
The text was updated successfully, but these errors were encountered: