Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upSubject 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? |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

We are logging activity of several models, some using SoftDeletes trait and others not.
When we set config option
'subject_returns_soft_deleted_models' => truethen 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