<!-- Syntax highlighted by torchlight.dev --><?php namespace App\Providers; use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { public function boot(): void { JsonResource::withoutWrapping(); } }
: withoutWrapping
haben
<!-- Syntax highlighted by torchlight.dev --><?php namespace App\Http\Resources; use Illuminate\Http\Resources\Json\JsonResource; class ArticleResource extends JsonResource { public function toArray($request) { return [ 'id' => $this->id, 'title' => $this->title, 'content' => $this->content, 'author' => new AuthorResource($this->whenLoaded('author')), 'metadata' => [ 'views' => $this->views_count, 'likes' => $this->likes_count, 'published_at' => $this->published_at ] ]; } }
<!-- Syntax highlighted by torchlight.dev -->[ { "id": 1, "title": "Laravel Tips", "content": "Article content here", "author": { "id": 1, "name": "John Doe", "email": "john@example.com" }, "metadata": { "views": 150, "likes": 42, "published_at": "2024-03-15T10:00:00Z" } } ]
Ressourcenverpackung kann weltweit deaktiviert werden und gleichzeitig die körnige Kontrolle über die Antwortstruktur Ihrer API beibehalten, was zu intuitiveren und leichteren Endpunkten führt.
Das obige ist der detaillierte Inhalt vonVerwenden ohne Schalten, um API -Antworten zu verflachten. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!