|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin\Grid\Displayers; |
|
4
|
|
|
|
|
5
|
|
|
use Encore\Admin\Admin; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany as Relation; |
|
7
|
|
|
use Illuminate\Support\Arr; |
|
8
|
|
|
|
|
9
|
|
|
class BelongsToMany extends BelongsTo |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Other key for many-to-many relation. |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected static $otherKey = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Get other key for this many-to-many relation. |
|
20
|
|
|
* |
|
21
|
|
|
* @throws \Exception |
|
22
|
|
|
* |
|
23
|
|
|
* @return string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected function getOtherKey() |
|
26
|
|
|
{ |
|
27
|
|
|
if (isset(static::$otherKey[$this->getName()])) { |
|
28
|
|
|
return static::$otherKey[$this->getName()]; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$model = $this->getGrid()->model()->getOriginalModel(); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
if (is_callable([$model, $this->getName()]) && |
|
34
|
|
|
($relation = $model->{$this->getName()}()) instanceof Relation |
|
35
|
|
|
) { |
|
36
|
|
|
/* @var Relation $relation */ |
|
37
|
|
|
$fullKey = $relation->getQualifiedRelatedPivotKeyName(); |
|
38
|
|
|
$fullKeyArray = explode('.', $fullKey); |
|
39
|
|
|
|
|
40
|
|
|
return static::$otherKey[$this->getName()] = end($fullKeyArray); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
throw new \Exception('Column of this field must be a `BelongsToMany` relation.'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @throws \Exception |
|
48
|
|
|
* |
|
49
|
|
|
* @return false|string|void |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function getOriginalData() |
|
52
|
|
|
{ |
|
53
|
|
|
$relations = $this->getColumn()->getOriginal(); |
|
54
|
|
|
|
|
55
|
|
|
if (is_string($relations)) { |
|
56
|
|
|
$data = explode(',', $relations); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (!is_array($relations)) { |
|
60
|
|
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$first = current($relations); |
|
64
|
|
|
|
|
65
|
|
|
if (is_null($first)) { |
|
66
|
|
|
$data = null; |
|
67
|
|
|
|
|
68
|
|
|
// MultipleSelect value store as an ont-to-many relationship. |
|
69
|
|
|
} elseif (is_array($first)) { |
|
70
|
|
|
foreach ($relations as $relation) { |
|
71
|
|
|
$data[] = Arr::get($relation, "pivot.{$this->getOtherKey()}"); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// MultipleSelect value store as a column. |
|
75
|
|
|
} else { |
|
76
|
|
|
$data = $relations; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return json_encode($data); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
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: