5

I have a polymorphic relationship set up in an OrderItem model, where saleable can be a few different models.

I've set it up like any other relationship:

public function saleable()
{
    return $this->morphTo()->withTrashed();
}

This used to work fine, now all of a sudden it doesn't work and it throws the error:

Call to undefined method Illuminate\Database\Query\Builder::withTrashed()

I don't understand why it would have stopped working, possibly due to a composer update which may have updated Laravel. I'm on v5.2.35. Any idea why it would have stopped working and what other solutions do I have?

UPDATE

Works fine with v5.2.33 and earlier. May be a bug, I've opened an issue on github

Wasim
  • 4,953
  • 10
  • 52
  • 87

2 Answers2

8

I dug through the code and found a solution! This solution is for my issue, where some models can be soft deleted and others cannot.

use Illuminate\Database\Eloquent\SoftDeletingScope;
//...
public function mySometimesSoftDeletableRelation(){
    return $this->morphTo()->withoutGlobalScope(SoftDeletingScope::class);
}

At its core, withTrashed uses unset, which doesn't care if it's unsetting something which isn't set, so this removes the SoftDeletingScope on those models which have it, and does nothing for others.

Chris Tyler
  • 273
  • 4
  • 7
  • i have this problem again in laravel 8.0, even tho it was stated this was fixed in 5.2 or so. this solution helped me. – narrei Jul 23 '23 at 17:23
2

The only thing I could imagine is that you removed SoftDelete trait for the object, so it's not loaded any more or you've added boot method that doesn't inherit from parent (so it won't load proper methods from this trait)

EDIT

It seems to be a bug introduced in v5.2.34. It seems to be fixed in this PR https://github.com/laravel/framework/pull/13828

EDIT

It should be fixed at the moment. v5.2.36 has been released.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • Thanks for the response. Each `saleable` model has the SoftDeletes trail, I wouldn't need one on the actual `OrderItem` itself as it's not relevant. I haven't added a `boot` method to the models either. – Wasim Jun 05 '16 at 19:57
  • @Wasim Have you changed any code before updating to latest version? Do you remember what version of 5.2 were you using before update? – Marcin Nabiałek Jun 05 '16 at 19:59
  • To be honest I haven't tested that bit of code in a while so I wouldn't be sure. Let me rollback laravel framework version and see if I can get it working – Wasim Jun 05 '16 at 20:06
  • Ok it breaks with version 5.2.34. Works fine with 5.2.33 and earlier. I can't see this being removed without notice so I assume it must be a bug? – Wasim Jun 05 '16 at 20:09
  • 1
    @Wasim I think you are right. It's a bug. when you look at changelog https://github.com/laravel/framework/blob/5.2/CHANGELOG.md for 5.2.34 you will see "Apply constraints to morphTo relationships when using eager loading" . Probably it has been fixed here: https://github.com/laravel/framework/pull/13828/commits but it's not released as 5.2.36 yet. – Marcin Nabiałek Jun 05 '16 at 20:28
  • @Wasim 5.2.36 has been released. Is the bug fixed when you update to this version? – Marcin Nabiałek Jun 07 '16 at 04:59