Complete the code to define a basic API route in Laravel.
Route::[1]('/users', function () { return User::all(); });
The get method defines a route that responds to HTTP GET requests, which is typical for fetching data via an API.
Complete the code to return JSON data from a Laravel API controller.
public function index() {
$users = User::all();
return [1]::json($users);
}Laravel's Response::json() method formats data as JSON, which is the standard for APIs.
Fix the error in the API route definition to correctly use a controller method.
Route::get('/posts', '[1]@index');
Controller class names in Laravel are case-sensitive and usually use PascalCase, so PostController is correct.
Fill both blanks to create a Laravel API resource route for 'products'.
Route::[1]('products', [2]::class);
apiResource creates API routes without web middleware, and ProductController is the correct controller name.
Fill all three blanks to define a Laravel API route group with middleware and prefix.
Route::[1](['middleware' => '[2]', 'prefix' => '[3]'], function () { Route::get('/orders', [OrderController::class, 'index']); });
The group method groups routes, auth:sanctum is middleware for API authentication, and api is a common prefix for API routes.