0
0
Laravelframework~10 mins

API resource controllers in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - API resource controllers
Client sends HTTP request
Route matches resource controller method
Controller method executes
index
Fetch data
Return JSON response to client
The flow shows how HTTP requests map to controller methods that handle data and return JSON responses.
Execution Sample
Laravel
Route::apiResource('books', BookController::class);

// BookController methods:
public function index() { return Book::all(); }
public function show($id) { return Book::findOrFail($id); }
Defines API routes for books and shows how index and show methods return data.
Execution Table
StepHTTP RequestRoute MatchedController MethodActionResponse
1GET /booksbooks.indexindex()Fetch all booksJSON list of all books
2GET /books/3books.showshow(3)Fetch book with ID 3JSON data of book 3
3POST /booksbooks.storestore()Validate & save new bookJSON data of created book
4PUT /books/3books.updateupdate(3)Validate & update book 3JSON data of updated book
5DELETE /books/3books.destroydestroy(3)Delete book 3204 No Content response
6GET /books/999books.showshow(999)Book 999 not found404 Not Found error
💡 Execution stops after sending JSON response or error to client.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6
booksempty[book1, book2, book3][book1, book2, book3][book1, book2, book3, newBook][book1, book2, updatedBook, newBook][book1, book2, newBook][book1, book2, newBook]
bookIdnullnull3null33999
responsenullJSON listJSON book 3JSON new bookJSON updated book204 No Content404 Not Found
Key Moments - 3 Insights
Why does the show method return a 404 error for a missing ID?
Because findOrFail throws an error if the book is not found, as shown in step 6 of the execution_table.
How does Laravel know which controller method to call for each HTTP request?
The apiResource route maps HTTP verbs and URLs to controller methods automatically, as seen in the Route Matched column.
What response does the destroy method send after deleting a record?
It sends a 204 No Content response, indicating success without returning data, shown in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what response does the index method return at step 1?
AJSON list of all books
BJSON data of book 3
C204 No Content response
D404 Not Found error
💡 Hint
Check the Response column at step 1 in the execution_table.
At which step does the controller return a 404 error?
AStep 2
BStep 6
CStep 4
DStep 5
💡 Hint
Look for 404 Not Found in the Response column of the execution_table.
If a new book is added, how does the 'books' variable change after step 3?
AIt stays the same
BIt removes a book
CIt adds the new book to the list
DIt becomes empty
💡 Hint
Check the variable_tracker row for 'books' after step 3.
Concept Snapshot
API resource controllers map HTTP verbs and URLs to controller methods.
Common methods: index, show, store, update, destroy.
They handle data fetching, validation, saving, updating, and deleting.
Responses are JSON or HTTP status codes.
Routes are defined with Route::apiResource.
Missing records return 404 errors automatically.
Full Transcript
API resource controllers in Laravel connect HTTP requests to controller methods automatically using Route::apiResource. When a client sends a request like GET /books, Laravel calls the index method to fetch all books and returns them as JSON. For GET /books/3, it calls show(3) to fetch one book or returns a 404 error if not found. POST requests call store to validate and save new data. PUT requests call update to modify existing data. DELETE requests call destroy to remove data and respond with 204 No Content. The execution table shows each step's request, matched route, controller method, action, and response. The variable tracker shows how data changes after each step. Key moments clarify why 404 errors happen, how routes map to methods, and what responses are sent after deletion. The visual quiz tests understanding of responses, error steps, and data changes. This helps beginners see how Laravel API resource controllers handle requests and responses step-by-step.