You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
API Platform version(s) affected: api-platform/laravel 4.0.3
Description
When attempting to use Data Transfer Objects (DTOs) with Laravel, as described in the documentation, I encounter a 404 error on the newly added route:
<?php
namespace App\ApiResource;
use ApiPlatform\Metadata\Get;
use App\State\CatProvider;
#[Get(uriTemplate: '/cats/{id}', provider: CatProvider::class)]
class Cat
{
public string $id;
public string $name;
public int $age;
}
Configure api-platform.php to include the DTO resource path:
Implement the CatProvider class that fetches data from the model:
<?php
namespace App\State;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Models\Animal as AnimalModel;
final class CatProvider implements ProviderInterface
{
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
$animal = AnimalModel::find($uriVariables['id']);
return new AnimalModel([
'id' => $animal->id,
'name' => $animal->name,
'age' => $animal->age
]);
}
}
Register the provider in the AppServiceProvider:
<?php
namespace App\Providers;
use App\State\CatProvider;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Foundation\Application;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
$this->app->singleton(CatProvider::class, function (Application $app) {
return new CatProvider();
});
$this->app->tag([CatProvider::class], 'provider');
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
//
}
}
Expected Behavior
The GET /cats/{id} route should return the correct data for the Cat resource using the CatProvider.
The text was updated successfully, but these errors were encountered:
There is an error in the following documentation. You need in your CatProvider to return the DTO ApiResource/Cat.php and not the model Models/Animal.php.
However, if you still want to do this you need to add the $fillable property to your Dto, like this:
API Platform version(s) affected: api-platform/laravel 4.0.3
Description
When attempting to use Data Transfer Objects (DTOs) with Laravel, as described in the documentation, I encounter a 404 error on the newly added route:
How to reproduce
api-platform.php
to include the DTO resource path:CatProvider
class that fetches data from the model:Expected Behavior
The
GET /cats/{id}
route should return the correct data for theCat
resource using the CatProvider.The text was updated successfully, but these errors were encountered: