1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Huntie\JsonApi\Http\Concerns; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
7
|
|
|
|
8
|
|
|
trait UpdatesModelRelations |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The model relationships that can be updated. |
12
|
|
|
* |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $fillableRelations = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Determine whether the given named relation can be updated on the model. |
19
|
|
|
* |
20
|
|
|
* @param string $relation |
21
|
|
|
*/ |
22
|
|
|
protected function isFillableRelation(string $relation): bool |
23
|
|
|
{ |
24
|
|
|
return array_key_exists($relation, $this->fillableRelations); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Determine the JSON API relation type for a named relation on the primary |
29
|
|
|
* resource. |
30
|
|
|
* |
31
|
|
|
* @param string $relation |
32
|
|
|
* |
33
|
|
|
* @return string|void |
34
|
|
|
*/ |
35
|
|
|
protected function getRelationType(string $relation) |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$relation = $this->model->{$name}(); |
|
|
|
|
38
|
|
|
|
39
|
|
|
if ($relation instanceof BelongsTo) { |
40
|
|
|
return 'To-One'; |
41
|
|
|
} else if ($relation instanceof BelongsToMany) { |
42
|
|
|
return 'To-Many'; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Update a named many-to-one relationship association on a model instance. |
48
|
|
|
* |
49
|
|
|
* http://jsonapi.org/format/#crud-updating-to-one-relationships |
50
|
|
|
* |
51
|
|
|
* @param Model $record |
52
|
|
|
* @param string $relation |
53
|
|
|
* @param array $data |
54
|
|
|
*/ |
55
|
|
|
protected function updateToOneResourceRelationship($record, string $relation, array $data) |
56
|
|
|
{ |
57
|
|
|
$record->{$relation}()->associate($data['id']); |
58
|
|
|
$record->save(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Update named many-to-many relationship entries on a model instance. |
63
|
|
|
* |
64
|
|
|
* http://jsonapi.org/format/#crud-updating-to-many-relationships |
65
|
|
|
* |
66
|
|
|
* @param Model $record |
67
|
|
|
* @param string $relation |
68
|
|
|
* @param array $data |
69
|
|
|
* @param string $method |
70
|
|
|
*/ |
71
|
|
|
protected function updateToManyResourceRelationship($record, string $relation, array $data, string $method) |
72
|
|
|
{ |
73
|
|
|
$items = []; |
74
|
|
|
|
75
|
|
|
foreach ($data as $item) { |
76
|
|
|
if (isset($item['attributes'])) { |
77
|
|
|
$items[$item['id']] = $item['attributes']; |
78
|
|
|
} else { |
79
|
|
|
$items[] = $item['id']; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
switch ($method) { |
84
|
|
|
case 'PATCH': |
85
|
|
|
$record->{$relation}()->sync($items); |
86
|
|
|
break; |
87
|
|
|
case 'POST': |
88
|
|
|
$record->{$relation}()->sync($items, false); |
89
|
|
|
break; |
90
|
|
|
case 'DELETE': |
91
|
|
|
$record->{$relation}()->detach(array_keys($items)); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Update one or more relationships on a model instance from an array of |
97
|
|
|
* named relationships and associated resource identifiers. |
98
|
|
|
* |
99
|
|
|
* http://jsonapi.org/format/#crud-updating-resource-relationships |
100
|
|
|
* |
101
|
|
|
* @param Model $record |
102
|
|
|
* @param array $relationships |
103
|
|
|
*/ |
104
|
|
|
protected function updateResourceRelationships($record, array $relationships) |
105
|
|
|
{ |
106
|
|
|
$relationships = array_intersect_key($relationships, array_flip($this->fillableRelations)); |
107
|
|
|
|
108
|
|
|
foreach ($relationships as $name => $relationship) { |
109
|
|
|
if ($this->getRelationType($relation) === 'To-One') { |
|
|
|
|
110
|
|
|
$record->{$name}()->associate(array_get($relationship, 'data.id')); |
111
|
|
|
$record->save(); |
112
|
|
|
} else if ($this->getRelationType($relation) === 'To-Many') { |
|
|
|
|
113
|
|
|
$record->{$name}()->sync(array_pluck($relationship['data'], 'id')); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.