1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: admin |
5
|
|
|
* Date: 09.05.2018 |
6
|
|
|
* Time: 11:47 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace sonrac\Arango\Eloquent\Reletations; |
10
|
|
|
|
11
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany as BelongsToManyBase; |
12
|
|
|
|
13
|
|
|
class BelongsToMany extends BelongsToManyBase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
*/ |
18
|
|
|
public function attach($id, array $attributes = [], $touch = true) |
19
|
|
|
{ |
20
|
|
|
// Here we will insert the attachment records into the pivot table. Once we have |
21
|
|
|
// inserted the records, we will touch the relationships if necessary and the |
22
|
|
|
// function will return. We can parse the IDs before inserting the records. |
23
|
|
|
$this->newPivotStatement()->insert($this->formatAttachRecords( |
24
|
|
|
$this->parseIds($id), $attributes |
25
|
|
|
)); |
26
|
|
|
|
27
|
|
|
if ($touch) { |
28
|
|
|
$this->touchIfTouching(); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
protected function attachNew(array $records, array $current, $touch = true) |
36
|
|
|
{ |
37
|
|
|
$changes = ['attached' => [], 'updated' => []]; |
38
|
|
|
|
39
|
|
|
foreach ($records as $id => $attributes) { |
40
|
|
|
$id = (string) $id; |
41
|
|
|
// If the ID is not in the list of existing pivot IDs, we will insert a new pivot |
42
|
|
|
// record, otherwise, we will just update this existing record on this joining |
43
|
|
|
// table, so that the developers will easily update these records pain free. |
44
|
|
|
if (!in_array($id, $current)) { |
45
|
|
|
$this->attach($id, $attributes, $touch); |
46
|
|
|
|
47
|
|
|
$changes['attached'][] = $id; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// Now we'll try to update an existing pivot record with the attributes that were |
51
|
|
|
// given to the method. If the model is actually updated we will add it to the |
52
|
|
|
// list of updated pivot records so we return them back out to the consumer. |
53
|
|
|
elseif (count($attributes) > 0 && |
54
|
|
|
$this->updateExistingPivot($id, $attributes, $touch)) { |
55
|
|
|
$changes['updated'][] = $id; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $changes; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
protected function formatRecordsList(array $records) |
66
|
|
|
{ |
67
|
|
|
$records = collect($records)->mapWithKeys(function ($attributes, $id) { |
68
|
|
|
if (!is_array($attributes)) { |
69
|
|
|
list($id, $attributes) = [$attributes, []]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return [$id => $attributes]; |
73
|
|
|
})->all(); |
74
|
|
|
|
75
|
|
|
return $records; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|