-
-
Notifications
You must be signed in to change notification settings - Fork 714
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
Subject should only return soft deleted models if that model uses SoftDeletes #456
Comments
The official way to get the class would be to resolve the Activity::whereHas('subject', function($query){ $query->where('active', true); })->get(); The only solution I could think about would be a simple try-catch block. |
@bluec or @MustafaHussaini can you make the added and linked unittest fail? laravel-activitylog/tests/ActivityModelTest.php Lines 146 to 179 in fb19f49
|
Hi @Gummibeer you described the behaviour exactly here: #445 (comment)
|
@Gummibeer I have done a PR #459 on your test to make it fail. You need to call |
Thanks for your PR! I will work on this. But that's even more strange. Because if I just do |
Thanks for looking into it. I've tried a ton of things including a simple try/catch block but I can't get anything to work. |
Ok, the problem why everything we do in the And this is why the initial call doesn't fail but pipe the method call I will do more research to find a way how we could prevent this. |
Ok, the real problem is that it's called during another query. I got a fix that works if the activity model is an instance with attributes: public function subject(): MorphTo
{
$morph = $this->morphTo();
if (config('activitylog.subject_returns_soft_deleted_models')) {
$query = $morph->createModelByType($this->{$morph->getMorphType()})->newQuery();
if(!is_null($query->getMacro('withTrashed'))) {
return $morph->withTrashed();
}
}
return $morph;
} But if the relation is loaded via So for now I really don't see a way how this could be fixed! 😖
My code above could be a good starting point for some of these solutions. |
Thanks for this. I didn't think there was an easy fix :( I initially added |
I'm also not a fan of them. No of my projects uses them for anything. |
if I understand the problem correctly then why don't you do it like below
|
@Mina-R-Meshriky thanks for your idea, I haven't checked it but so far I see it this would trigger two queries. One just to determine if it should use soft-deletes or not. At all there is no issue if you do I've tried to describe it above - the code way is:
There is no way to know in step 2 if any of the unknown subjects of the unknown activities uses soft-deletes or not. Because we allow to override all needed methods and to disable soft-deletes via config I don't see a reason to mess up with deep core logic just to allow mixed-soft-deletes-subjects. If I'm wrong and your example doesn't issue a new database request and it solves the issue I would be happy to apply your suggestion!? |
when I |
Have you done it after you got your activity instance or during the activity query? |
Dear contributor, because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it. |
I've run into this issue and it looks like a fix hasn't been found yet. Could you check for a deleted_at column on the model? public function subject(): MorphTo
{
if (config('activitylog.subject_returns_soft_deleted_models') && $this->deleted_at) {
return $this->morphTo()->withTrashed();
}
return $this->morphTo();
} |
hey @tomfortmuller , the columns doesn't have to exist on |
I run into same issue with a similar use case what I did was
If you want to do it on the relation load
|
This still seems to be an issue. Any progress on this? |
@MichMich So far I know the underlying issue isn't solved. So as long as no one finds a crazy hacky solution this won't change. 🤔 |
Can we not just use a try/catch here to fix this? Seems like the simplest solution to me |
Not really as the relation is/could be mixed. But I will spend some time to get again into all this and check it with current Laravel 8 - probably something has changed over time. |
Okay, nothing has changed - we have no way from the outside to manipulate the applied scopes based on the type of the morphed relation record. if(array_key_exists(SoftDeletes::class, class_uses_recursive($morphTo->getQuery()->getModel()))) {
dump(SoftDeletes::class);
} Here are both backtraces how the closure/macro in |
Can't you check if the model has a |
That's something the user could do but I don't see why we should "fix" this as it's not a bug with this package but a general morphed relation Laravel one. At all my recommendation is to don't mix soft and hard deletes in an app and think like a thousand times about the usage of soft deletes at all. Because in ~99% of the cases they are misused. |
Just an update, this issue is now resolved in Laravel Framework 10.20.0 |
We are logging activity of several models, some using SoftDeletes trait and others not.
When we set config option
'subject_returns_soft_deleted_models' => true
then callingsubject()
on any model not using SoftDeletes trait throws an exception:Call to undefined method Illuminate\Database\Eloquent\Builder::withTrashed()
.Can the
subject()
method below be made to check first whether the Model uses the SoftDeletes trait? Or perhaps check whetherwithTrashed()
is a defined method on the Model? I tried a few things but couldn't figure out how to get the class of the Model..laravel-activitylog/src/Models/Activity.php
Lines 28 to 35 in 68eb6e6
The text was updated successfully, but these errors were encountered: