Skip to content

Commit 66053e4

Browse files
committed
[Refactor] Use snake case if not camel casing model relation names
1 parent 8205613 commit 66053e4

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/Eloquent/AbstractAdapter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use CloudCreativity\LaravelJsonApi\Document\ResourceObject;
2727
use CloudCreativity\LaravelJsonApi\Exceptions\RuntimeException;
2828
use CloudCreativity\LaravelJsonApi\Pagination\CursorStrategy;
29+
use CloudCreativity\LaravelJsonApi\Utils\Str;
2930
use Illuminate\Database\Eloquent\Builder;
3031
use Illuminate\Database\Eloquent\Model;
3132
use Illuminate\Database\Eloquent\Relations;
@@ -622,7 +623,7 @@ private function guessRelation()
622623
{
623624
list($one, $two, $caller) = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
624625

625-
return $this->methodForRelation($caller['function']);
626+
return $this->modelRelationForField($caller['function']);
626627
}
627628

628629
}

src/Eloquent/Concerns/IncludesModels.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,10 @@ protected function convertIncludePath($path)
169169
*
170170
* - If `true`, a field name of `user-history` or `user_history` will
171171
* expect the Eloquent model relation method to be `userHistory`.
172-
* - If `false`, the field name will be expected to be identical to
173-
* the Eloquent method, i.e. `user_history` in both JSON API and on the
174-
* model. (For this scenario, `user-history` will not work as a field
175-
* name.)
172+
* - If `false`, a field name of `user-history` or `user_history` will
173+
* expect the Eloquent model relation method to be `user_history`. I.e.
174+
* if PSR1 is not being followed, the best guess is that method names
175+
* are snake case.
176176
*
177177
* If the developer has different conversion logic, they should overload
178178
* this method and implement it themselves.
@@ -184,6 +184,6 @@ protected function convertIncludePath($path)
184184
*/
185185
protected function modelRelationForField($field)
186186
{
187-
return $this->camelCaseRelations ? Str::camelize($field) : $field;
187+
return $this->camelCaseRelations ? Str::camelize($field) : Str::underscore($field);
188188
}
189189
}

0 commit comments

Comments
 (0)