Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 711a627

Browse files
committed
Merge pull request laravel#4464 from ambrella/4.2
[4.2] withTrashed() constraint does not work with morphTo() relations.
2 parents 901e6ea + 2603dda commit 711a627

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

src/Illuminate/Database/Eloquent/Relations/MorphTo.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ class MorphTo extends BelongsTo {
2828
*/
2929
protected $dictionary = array();
3030

31+
/*
32+
* Indicates if soft-deleted model instances must be fetched or not
33+
*
34+
* @var bool
35+
*/
36+
protected $withTrashed = false;
37+
3138
/**
3239
* Create a new belongs to relationship instance.
3340
*
@@ -150,6 +157,11 @@ protected function getResultsByType($type)
150157
{
151158
$instance = $this->createModelByType($type);
152159

160+
if ($this->withTrashed && $instance->newQuery()->getMacro('withTrashed') !== null)
161+
{
162+
$instance = $instance->withTrashed();
163+
}
164+
153165
$key = $instance->getKeyName();
154166

155167
return $instance->whereIn($key, $this->gatherKeysByType($type)->all())->get();
@@ -193,4 +205,16 @@ public function getDictionary()
193205
return $this->dictionary;
194206
}
195207

208+
/**
209+
* Fetch soft-deleted model instances with query
210+
*
211+
* @return MorphTo
212+
*/
213+
public function withTrashed()
214+
{
215+
$this->withTrashed = true;
216+
217+
return $this;
218+
}
219+
196220
}

0 commit comments

Comments
 (0)