1
|
|
|
<?php |
2
|
|
|
namespace Prateekkarki\Laragen\Models\Types\Relational\Multiple; |
3
|
|
|
use Prateekkarki\Laragen\Models\TypeResolver; |
4
|
|
|
use Illuminate\Support\Str; |
5
|
|
|
|
6
|
|
|
use Prateekkarki\Laragen\Models\Types\Relational\MultipleType; |
7
|
|
|
|
8
|
|
|
class RelatedType extends MultipleType |
9
|
|
|
{ |
10
|
|
|
protected $hasPivot = true; |
11
|
|
|
protected $formType = 'related'; |
12
|
|
|
protected $stubs = [ |
13
|
|
|
'foreignMethod' => 'common/Models/fragments/belongsToMany' |
14
|
|
|
]; |
15
|
|
|
|
16
|
|
|
public function getPivotSchema() |
17
|
|
|
{ |
18
|
|
|
$moduleName = $this->getParentModule(); |
19
|
|
|
$schema = PHP_EOL.$this->getTabs(3); |
20
|
|
|
$schema .= '$table->bigInteger("'.$this->getParentKey().'")->unsigned()->nullable();'.PHP_EOL.$this->getTabs(3); |
21
|
|
|
$schema .= '$table->foreign("'.$this->getParentKey().'")->references("id")->on("'.$moduleName.'")->onDelete("set null");'.PHP_EOL.$this->getTabs(3); |
22
|
|
|
|
23
|
|
|
$schema .= '$table->bigInteger("'.$this->getChildKey().'")->unsigned()->nullable();'.PHP_EOL.$this->getTabs(3); |
24
|
|
|
$schema .= '$table->foreign("'.$this->getChildKey().'")->references("id")->on("'.$this->typeOption.'")->onDelete("set null");'.PHP_EOL; |
25
|
|
|
|
26
|
|
|
return $schema; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getParentKey() |
30
|
|
|
{ |
31
|
|
|
return $this->getParentModelLowercase()."_id"; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getRelatedModel() |
35
|
|
|
{ |
36
|
|
|
return $this->getOptionModel(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getOptionModel() |
40
|
|
|
{ |
41
|
|
|
return ucfirst(Str::camel(Str::singular($this->typeOption))); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getChildKey() |
45
|
|
|
{ |
46
|
|
|
return Str::singular($this->columnName)."_id"; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getChildModel() |
50
|
|
|
{ |
51
|
|
|
return ucfirst(Str::camel(Str::singular($this->columnName))); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getPivotTable() |
55
|
|
|
{ |
56
|
|
|
$modelArray = [$this->getParentModelLowercase(), strtolower(Str::singular($this->columnName))]; |
57
|
|
|
sort($modelArray); |
58
|
|
|
return implode("_", $modelArray); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getMigrationPivot() |
62
|
|
|
{ |
63
|
|
|
$modelArray = [$this->getParentModel(), $this->getChildModel()]; |
64
|
|
|
sort($modelArray); |
65
|
|
|
return implode("", $modelArray); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getPivot() |
69
|
|
|
{ |
70
|
|
|
$modelArray = [$this->getParentModel(), $this->getChildModel()]; |
71
|
|
|
sort($modelArray); |
72
|
|
|
return implode("", $modelArray); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getPivotColumns() |
76
|
|
|
{ |
77
|
|
|
$columnModels = []; |
78
|
|
|
$columns = [ |
79
|
|
|
$this->getParentModelLowercase() => 'parent:'.$this->getParentModule(), |
80
|
|
|
Str::singular($this->columnName) => 'parent:'.$this->typeOption, |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
foreach ($columns as $column => $optionString) { |
84
|
|
|
$columnModels[$column] = TypeResolver::getType($this->getPivotTable(), $column, $optionString); |
85
|
|
|
} |
86
|
|
|
return $columnModels; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getTypeColumns() |
90
|
|
|
{ |
91
|
|
|
return [$this->getParentModelLowercase().'_id', strtolower(Str::singular($this->typeOption)).'_id']; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|