1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Show; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Grid; |
6
|
|
|
use Encore\Admin\Show; |
7
|
|
|
use Illuminate\Contracts\Support\Renderable; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
10
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
11
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
12
|
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough; |
13
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne; |
14
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
15
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne; |
16
|
|
|
|
17
|
|
|
class Relation extends Field |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Relation name. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $name; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Relation panel builder. |
28
|
|
|
* |
29
|
|
|
* @var callable |
30
|
|
|
*/ |
31
|
|
|
protected $builder; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Relation panel title. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $title; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Parent model. |
42
|
|
|
* |
43
|
|
|
* @var Model |
44
|
|
|
*/ |
45
|
|
|
protected $model; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Relation constructor. |
49
|
|
|
* |
50
|
|
|
* @param string $name |
51
|
|
|
* @param callable $builder |
52
|
|
|
* @param string $title |
53
|
|
|
*/ |
54
|
|
|
public function __construct($name, $builder, $title = '') |
55
|
|
|
{ |
56
|
|
|
$this->name = $name; |
57
|
|
|
$this->builder = $builder; |
58
|
|
|
$this->title = $this->formatLabel($title); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set parent model for relation. |
63
|
|
|
* |
64
|
|
|
* @param Model $model |
65
|
|
|
* |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
|
|
public function setModel(Model $model) |
69
|
|
|
{ |
70
|
|
|
$this->model = $model; |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get null renderable instance. |
77
|
|
|
* |
78
|
|
|
* @return Renderable|__anonymous@1539 |
|
|
|
|
79
|
|
|
*/ |
80
|
|
|
protected function getNullRenderable() |
81
|
|
|
{ |
82
|
|
|
return new class implements Renderable { |
83
|
|
|
public function render() |
84
|
|
|
{ |
85
|
|
|
} |
86
|
|
|
}; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Render this relation panel. |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function render() |
95
|
|
|
{ |
96
|
|
|
$relation = $this->model->{$this->name}(); |
97
|
|
|
|
98
|
|
|
$renderable = $this->getNullRenderable(); |
99
|
|
|
|
100
|
|
|
if ($relation instanceof HasOne |
101
|
|
|
|| $relation instanceof BelongsTo |
102
|
|
|
|| $relation instanceof MorphOne |
103
|
|
|
) { |
104
|
|
|
$model = $this->model->{$this->name}; |
105
|
|
|
|
106
|
|
|
if (!$model instanceof Model) { |
107
|
|
|
$model = $relation->getRelated(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$renderable = new Show($model, $this->builder); |
111
|
|
|
|
112
|
|
|
$renderable->panel()->title($this->title); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($relation instanceof HasMany |
116
|
|
|
|| $relation instanceof MorphMany |
117
|
|
|
|| $relation instanceof BelongsToMany |
118
|
|
|
|| $relation instanceof HasManyThrough |
119
|
|
|
) { |
120
|
|
|
$renderable = new Grid($relation->getRelated(), $this->builder); |
|
|
|
|
121
|
|
|
|
122
|
|
|
$renderable->setName($this->name) |
123
|
|
|
->setTitle($this->title) |
124
|
|
|
->setRelation($relation); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $renderable->render(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.