Skip to content

Commit f283fc5

Browse files
samatcdphpsa
andauthored
Parse sync fields (#84)
* Parse sync fields + Properly use case mapping on sync related fields. * Apply fixes from StyleCI (#85) Co-authored-by: Craig Smith <[email protected]> Co-authored-by: Craig Smith <[email protected]> Co-authored-by: Craig Smith <[email protected]>
1 parent 23def87 commit f283fc5

File tree

4 files changed

+12
-15
lines changed

4 files changed

+12
-15
lines changed

src/Contracts/Parser.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,10 @@ protected function parseJoinSorts(Collection $sorts)
147147

148148
$withTable = $relation->getRelated()->getTable();
149149

150-
$withTableName = strpos($withTable, ".") === false ? $withConnection . '.' . $withTable : $withTable;
150+
$withTableName = strpos($withTable, '.') === false ? $withConnection.'.'.$withTable : $withTable;
151151

152152
$this->repository->join($withTableName, "{$withTableName}.{$foreignKey}", "{$currentTable}.{$localKey}");
153153
$this->repository->orderBy("{$withTableName}.{$key}", $sortD);
154-
155154
}
156155
}
157156

src/Contracts/Relationships.php

+7-10
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,13 @@ protected function processBelongsToManyRelation($relation, array $relatedRecords
183183
$relatedKey = $relation->getRelatedKeyName();
184184

185185
$model = $relation->getRelated();
186-
$sync = collect();
186+
$pivots = $relation->getPivotColumns();
187187

188-
$pivots = $relation->getPivotColumns();
189-
$detach = filter_var(request()->get('sync')[$with] ?? false, FILTER_VALIDATE_BOOLEAN);
188+
$syncWithRelated = Helpers::snakeCaseArrayKeys(request()->get('sync') ?? []);
189+
$detach = filter_var($syncWithRelated[Helpers::snake($with)] ?? false, FILTER_VALIDATE_BOOLEAN);
190+
$sync = collect();
190191

191192
foreach ($relatedRecords as $relatedRecord) {
192-
193193
if (! isset($relatedRecord[$parentKey])) {
194194
$relatedRecord[$parentKey] = $item->getAttribute($relatedKey);
195195
}
@@ -204,17 +204,14 @@ protected function processBelongsToManyRelation($relation, array $relatedRecords
204204
}
205205

206206
$pvals = [];
207-
if($pivots){
208-
209-
foreach($pivots as $pivot)
210-
{
211-
if(isset($relatedRecord[$pivot])){
207+
if ($pivots) {
208+
foreach ($pivots as $pivot) {
209+
if (isset($relatedRecord[$pivot])) {
212210
$pvals[$pivot] = $relatedRecord[$pivot];
213211
}
214212
}
215213
}
216214
$sync->put($record->getKey(), $pvals);
217-
218215
}
219216

220217
$relation->sync($sync->toArray(), $detach);

src/Helpers.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,19 @@ public static function filterFieldsFromRequest($request, ?array $defaultFields,
162162
protected static function fieldsFromPutPost($request, $fields): array
163163
{
164164
$method = $request->method();
165-
if (! in_array($method, ['PUT','POST','PATCH'])) {
165+
if (! in_array($method, ['PUT', 'POST', 'PATCH'])) {
166166
return [];
167167
}
168+
168169
return array_values(collect($request->all())->filter(function ($item, $key) use ($fields) {
169170
if (in_array($key, $fields)) {
170171
return false;
171172
}
172173
if (! is_array($item) && ! is_object($key)) {
173174
return false;
174175
}
175-
return true;
176+
177+
return true;
176178
})->map(function ($item, $key) {
177179
return $key;
178180
})->toArray());

src/Http/Api/Contracts/HasResponse.php

-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ protected function errorNotImplemented($message = 'Not implemented', ?array $err
243243
throw new HttpException(501, $message);
244244
}
245245

246-
247246
protected function handleIndexResponse($items)
248247
{
249248
return $this->respondWithMany($items);

0 commit comments

Comments
 (0)