1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BBSLab\NovaTranslation\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Collection; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
8
|
|
|
use Illuminate\Database\Query\Builder; |
9
|
|
|
use Illuminate\Support\Facades\DB; |
10
|
|
|
|
11
|
|
|
class TranslationRelation extends Relation |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var \BBSLab\NovaTranslation\Models\Translation|\Illuminate\Database\Eloquent\Builder |
15
|
|
|
*/ |
16
|
|
|
protected $query; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable |
20
|
|
|
*/ |
21
|
|
|
protected $parent; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \BBSLab\NovaTranslation\Models\Translation |
25
|
|
|
*/ |
26
|
|
|
protected $related; |
27
|
|
|
|
28
|
|
|
public function __construct(Model $parent) |
29
|
|
|
{ |
30
|
|
|
parent::__construct(Translation::query(), $parent); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function addConstraints() |
37
|
|
|
{ |
38
|
|
|
if (static::$constraints) { |
39
|
|
|
$this->query |
|
|
|
|
40
|
|
|
->where('translatable_id', '<>', $this->parent->getKey()) |
41
|
|
|
->where('translatable_type', '=', get_class($this->parent)) |
42
|
|
View Code Duplication |
->whereExists(function (Builder $query) { |
|
|
|
|
43
|
|
|
$query->select(DB::raw(1)) |
44
|
|
|
->from('translations as original') |
45
|
|
|
->whereRaw('original.translation_id = translations.translation_id') |
46
|
|
|
->where('original.translatable_id', '=', $this->parent->getKey()) |
47
|
|
|
->where('original.translatable_type', '=', get_class($this->parent)); |
48
|
|
|
}); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function addEagerConstraints(array $models) |
56
|
|
|
{ |
57
|
|
|
$ids = collect($models)->pluck($this->parent->getKeyName()); |
58
|
|
|
|
59
|
|
|
$this->query |
60
|
|
|
->whereNotIn('translatable_id', $ids) |
61
|
|
|
->where('translatable_type', '=', get_class($this->parent)) |
62
|
|
View Code Duplication |
->whereExists(function (Builder $query) use ($ids) { |
|
|
|
|
63
|
|
|
$query->select(DB::raw(1)) |
64
|
|
|
->from('translations as original') |
65
|
|
|
->whereRaw('translations.translation_id = original.translation_id') |
66
|
|
|
->whereIn('original.translatable_id', $ids) |
67
|
|
|
->where('original.translatable_type', '=', get_class($this->parent)); |
68
|
|
|
}); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function initRelation(array $models, $relation) |
75
|
|
|
{ |
76
|
|
|
foreach ($models as $model) { |
77
|
|
|
/** @var \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable $model */ |
78
|
|
|
$model->setRelation($relation, $this->related->newCollection()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $models; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function match(array $models, Collection $results, $relation) |
88
|
|
|
{ |
89
|
|
|
if ($results->isEmpty()) { |
90
|
|
|
return $models; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
foreach ($models as $model) { |
94
|
|
|
/** @var \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable $model */ |
95
|
|
|
$model->setRelation( |
96
|
|
|
$relation, |
97
|
|
|
$results->filter(function (Translation $translation) use ($model) { |
98
|
|
|
return $translation->translation_id === $model->translation->translation_id |
|
|
|
|
99
|
|
|
&& $translation->translatable_id !== $model->getKey(); |
100
|
|
|
}) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $models; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
|
public function getResults() |
111
|
|
|
{ |
112
|
|
|
return ! is_null($this->getParent()->getKey()) |
113
|
|
|
? $this->query->get() |
|
|
|
|
114
|
|
|
: $this->related->newCollection(); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: