|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PrismX\Schemaless\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use PrismX\Schemaless\Document; |
|
6
|
|
|
|
|
7
|
|
|
trait HasSchemalessRelationships |
|
8
|
|
|
{ |
|
9
|
|
|
public function itemable(string $class = Document::class) |
|
10
|
|
|
{ |
|
11
|
|
|
return $this->morphTo($this->getItemMorphColumnName()) |
|
|
|
|
|
|
12
|
|
|
->where($this->getItemMorphColumnName().'_type', $class); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphOne |
|
17
|
|
|
*/ |
|
18
|
|
|
public function item(string $class = Document::class) |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
return $this->morphOne(Document::class, $this->getItemMorphColumnName()); |
|
|
|
|
|
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
25
|
|
|
*/ |
|
26
|
|
|
public function items(string $class = Document::class) |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
return $this->morphMany(Document::class, $this->getItemMorphColumnName()); |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
|
33
|
|
|
*/ |
|
34
|
|
|
public function manyItems(string $class = Document::class) |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->morphToMany( |
|
|
|
|
|
|
37
|
|
|
$class, |
|
38
|
|
|
'child', |
|
39
|
|
|
'item_items', |
|
40
|
|
|
'child_id', |
|
41
|
|
|
'parent_id' |
|
42
|
|
|
) |
|
43
|
|
|
->withPivot('parent_type', 'parent_id', 'child_type', 'child_id') |
|
44
|
|
|
->wherePivot('parent_type', get_class($this)) |
|
45
|
|
|
->withPivotValue('parent_type', get_class($this)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
|
50
|
|
|
*/ |
|
51
|
|
|
public function byManyItems(string $class = Document::class) |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->morphedByMany( |
|
|
|
|
|
|
54
|
|
|
$class, |
|
55
|
|
|
'parent', |
|
56
|
|
|
'item_items', |
|
57
|
|
|
'parent_id', |
|
58
|
|
|
'child_id' |
|
59
|
|
|
) |
|
60
|
|
|
->withPivot('parent_type', 'parent_id', 'child_type', 'child_id') |
|
61
|
|
|
->wherePivot('child_type', get_class($this)) |
|
62
|
|
|
->withPivotValue('child_type', get_class($this)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @override |
|
67
|
|
|
* |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getItemMorphColumnName() |
|
71
|
|
|
{ |
|
72
|
|
|
return 'itemable'; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|