|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Akibatech\Crud\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Akibatech\Crud\Exceptions\InvalidRelationException; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
|
7
|
|
|
use Illuminate\Support\Fluent; |
|
8
|
|
|
use Illuminate\Validation\Validator; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class FieldWithRelation |
|
12
|
|
|
* |
|
13
|
|
|
* @package Akibatech\Crud\Traits |
|
14
|
|
|
*/ |
|
15
|
|
|
trait FieldWithRelation |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string $relation |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $relation; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $options; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $relation |
|
29
|
|
|
* @return self |
|
30
|
|
|
*/ |
|
31
|
|
|
public function withRelation($relation) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->relation = $relation; |
|
34
|
|
|
|
|
35
|
|
|
return $this; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param void |
|
40
|
|
|
* @return Relation |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getRelation() |
|
43
|
|
|
{ |
|
44
|
|
|
if (empty($this->relation)) |
|
45
|
|
|
{ |
|
46
|
|
|
throw new InvalidRelationException("{$this->getIdentifier()} does not contain any relation."); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$relation = $this->relation; |
|
50
|
|
|
|
|
51
|
|
|
return $this->getFields()->getEntry()->getModel()->$relation(); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getOptions() |
|
58
|
|
|
{ |
|
59
|
|
|
if (is_null($this->options)) |
|
60
|
|
|
{ |
|
61
|
|
|
$related = get_class($this->getRelation()->getRelated()); |
|
62
|
|
|
$options = $related::get()->pluck('name', 'id')->toArray(); |
|
63
|
|
|
|
|
64
|
|
|
$this->options = $options; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $this->options; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param Validator $validator |
|
72
|
|
|
* @return Validator |
|
73
|
|
|
*/ |
|
74
|
|
|
public function beforeValidation(Validator $validator) |
|
75
|
|
|
{ |
|
76
|
|
|
$related_class = $this->getRelation()->getRelated(); |
|
77
|
|
|
$related_instance = new $related_class; |
|
78
|
|
|
$related_table = $related_class->getTable(); |
|
79
|
|
|
$related_pk = $related_instance->getKeyName(); |
|
80
|
|
|
|
|
81
|
|
|
$validator->mergeRules($this->getIdentifier(), [ |
|
|
|
|
|
|
82
|
|
|
"exists:$related_table,$related_pk" |
|
83
|
|
|
]); |
|
84
|
|
|
|
|
85
|
|
|
return $validator; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getTableValue() |
|
92
|
|
|
{ |
|
93
|
|
|
$relation = $this->relation; |
|
94
|
|
|
return $this->getFields()->getEntry()->getModel()->$relation->name; |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritdoc} |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getViewVariables() |
|
101
|
|
|
{ |
|
102
|
|
|
return [ |
|
103
|
|
|
'options' => $this->getOptions(), |
|
104
|
|
|
]; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.