|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MaksimM\CompositePrimaryKeys\Eloquent\Relationships; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
|
8
|
|
|
use Illuminate\Support\Collection as BaseCollection; |
|
9
|
|
|
use MaksimM\CompositePrimaryKeys\Http\Traits\CompositeRelationships; |
|
10
|
|
|
|
|
11
|
|
|
class CompositeBelongsToMany extends BelongsToMany |
|
12
|
|
|
{ |
|
13
|
|
|
use CompositeRelationships; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Get all of the IDs from the given mixed value. |
|
17
|
|
|
* |
|
18
|
|
|
* @param mixed $value |
|
19
|
|
|
* |
|
20
|
|
|
* @return array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected function parseIds($value) |
|
23
|
|
|
{ |
|
24
|
|
|
if ($value instanceof Model) { |
|
25
|
|
|
return [$this->executeWithinOptionalBinaryTransformation(function () use ($value) { |
|
26
|
|
|
return $value->{$this->relatedKey}; |
|
27
|
|
|
}, $value)]; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
if ($value instanceof Collection) { |
|
31
|
|
|
return $value->pluck($this->relatedKey)->all(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
if ($value instanceof BaseCollection) { |
|
35
|
|
|
return $value->toArray(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return (array) $value; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get the ID from the given mixed value. |
|
43
|
|
|
* |
|
44
|
|
|
* @param mixed $value |
|
45
|
|
|
* |
|
46
|
|
|
* @return mixed |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function parseId($value) |
|
49
|
|
|
{ |
|
50
|
|
|
return $value instanceof Model ? $this->executeWithinOptionalBinaryTransformation(function () use ($value) { |
|
51
|
|
|
$value->{$this->relatedKey}; |
|
52
|
|
|
}, $value) : $value; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Create a new query builder for the pivot table. |
|
57
|
|
|
* |
|
58
|
|
|
* @return \Illuminate\Database\Query\Builder |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function newPivotQuery() |
|
61
|
|
|
{ |
|
62
|
|
|
$query = $this->newPivotStatement(); |
|
63
|
|
|
|
|
64
|
|
|
foreach ($this->pivotWheres as $arguments) { |
|
65
|
|
|
call_user_func_array([$query, 'where'], $arguments); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
foreach ($this->pivotWhereIns as $arguments) { |
|
69
|
|
|
call_user_func_array([$query, 'whereIn'], $arguments); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $this->executeWithinOptionalBinaryTransformation(function () use ($query) { |
|
73
|
|
|
return $query->where($this->foreignPivotKey, $this->parent->{$this->parentKey}); |
|
74
|
|
|
}, $this->parent); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Create a new pivot attachment record. |
|
79
|
|
|
* |
|
80
|
|
|
* @param int $id |
|
81
|
|
|
* @param bool $timed |
|
82
|
|
|
* @return array |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function baseAttachRecord($id, $timed) |
|
85
|
|
|
{ |
|
86
|
|
|
$record[$this->relatedPivotKey] = $id; |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
$record[$this->foreignPivotKey] = $this->executeWithinOptionalBinaryTransformation(function () { |
|
89
|
|
|
return $this->parent->{$this->parentKey}; |
|
90
|
|
|
}, $this->parent); |
|
91
|
|
|
|
|
92
|
|
|
// If the record needs to have creation and update timestamps, we will make |
|
93
|
|
|
// them by calling the parent model's "freshTimestamp" method which will |
|
94
|
|
|
// provide us with a fresh timestamp in this model's preferred format. |
|
95
|
|
|
if ($timed) { |
|
96
|
|
|
$record = $this->addTimestampsToAttachment($record); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
foreach ($this->pivotValues as $value) { |
|
100
|
|
|
$record[$value['column']] = $value['value']; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $record; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|