Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct BelongsTo relation creation #417

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Extracting/Strategies/Responses/UseApiResourceTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ protected function instantiateApiResourceModel(string $type, array $factoryState
try {
return $factory->create();
} catch (Throwable $e) {
c::warn("Eloquent model factory failed to create {$type}; trying to make it.");
e::dumpExceptionIfVerbose($e, true);

// If there was no working database, ->create() would fail. Try ->make() instead
return $factory->make();
}
Expand Down
3 changes: 3 additions & 0 deletions src/Tools/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use DirectoryIterator;
use Exception;
use FastRoute\RouteParser\Std;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Routing\Route;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -251,6 +252,8 @@ public static function getModelFactory(string $modelName, array $states = [], ar
: [];

$factory = $factory->hasAttached($factoryChain, $pivot, $relationVector);
} else if ($relationType === BelongsTo::class) {
$factory = $factory->for($factoryChain, $relationVector);
} else {
$factory = $factory->has($factoryChain, $relationVector);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Strategies/Responses/UseApiResourceTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -641,4 +641,9 @@ public function can_parse_apiresourcecollection_tags_with_collection_class_pagin
],
], $results);
}

protected function afterTestCleanup()
{
config(['scribe.database_connections_to_transact' => []]);
}
}