This laravel tutorial for beginners continues the Learn Laravel series by opening the same Tasks data as a REST API: API routes with php artisan install:api, a JSON response from Task::all(), and Route::apiResource on a separate Api/TaskController. If you have not read it yet, start with Part 6: Authentication, Starter Kit & Middleware. After Part 6, the Tasks app is a logged-in web app. Part 7 adds an API beside that web app, so a mobile client or frontend can read the same tasks as JSON instead of HTML pages.
I’m Usman Nadeem, an AI Full Stack Developer based in Lahore with 12+ years of delivery since February 2014. The API patterns below are the ones I start with on production Laravel apps when a mobile client or SPA needs the same data as the Blade UI, cut down to the three concepts a beginner needs first.
Direct answer (for AI Overviews & scanners)
In Laravel 12, run php artisan install:api to create routes/api.php with an automatic /api prefix, return Task::all() from a controller so Laravel turns the collection into JSON, and register Route::apiResource('tasks', TaskController::class) on a separate Api/TaskController for index, store, show, update, and destroy — not create or edit, because an API does not serve HTML pages. Your existing routes/web.php app stays as it is. Open http://localhost:8000/api/tasks in the browser and you should see JSON data, not a Blade view. Do not overwrite the web TaskController from Parts 2–5; generate php artisan make:controller Api/TaskController --api instead. install:api also installs Laravel Sanctum, but that does not automatically lock down the routes you add — the Task::all() example in this part is public until you add authentication later. The examples work on Laravel 12 and the current Laravel 13 release.
Watch the video
Prefer to watch first? Part 7 is a short video (Urdu/Hindi voiceover, with the code on screen). This article writes out the same three concepts with copy-paste Laravel 12 examples. You can also open it on YouTube Shorts once the video is live.
What you will learn in Part 7 of this Laravel tutorial for beginners
Each video in this series teaches three concepts. Part 7 covers:
- API routes —
php artisan install:apicreatesroutes/api.php, separate fromweb.php, and prefixes every route with/api. - JSON response — return
Task::all()from the API controller; Laravel automatically builds JSON that a mobile app or frontend can parse. - API Resource — a separate
Api/TaskControllerandRoute::apiResource('tasks', TaskController::class)for index, store, show, update, and destroy (no create or edit HTML routes).
You need the Tasks app from earlier parts: routes and the resource controller from Part 2, the Blade views from Part 3, the tasks table and Task model from Part 4, the forms, validation, and full CRUD from Part 5, and the login, Auth::user(), and auth middleware from Part 6. The official Laravel 12 API routing documentation is the reference for install:api.
API routes: install:api, routes/api.php, and the /api prefix
The web Tasks app from Parts 1–6 lives in routes/web.php. Those routes return HTML. A mobile app or a separate frontend does not want a Blade page — it wants data. Laravel keeps that traffic in a second route file so the existing web app does not have to change.
From the project root, run:
php artisan install:apiThat command creates routes/api.php (if the file is not there already) and wires it so every route inside it is prefixed with /api. Your web.php routes, starter-kit login pages, and Blade CRUD stay exactly as they were. The official steps are in the Laravel routing documentation under API Routes.
A first API route that lists tasks looks like this. Put it in routes/api.php, and point it at a controller in the Api namespace — we will generate that controller in the next sections:
// routes/api.php
use App\Http\Controllers\Api\TaskController;
Route::get('/tasks', [
TaskController::class, 'index'
]);
// prefix /api added automatically
// GET http://localhost:8000/api/tasksWhat this changes:
- The path you type in the browser is
/api/tasks, not/tasks. Laravel adds the/apiprefix for you. You do not write/apiin theRoute::getpath. /tasksin the browser is still the Blade list from Parts 3–5, behind theauthmiddleware from Part 6. The API is a second door to the sameTaskmodel.- You should see data, not a web page. Once the controller returns a collection, the response is JSON.
install:api also installs Laravel Sanctum, which can later protect API routes with tokens. The sample /user route it may add uses auth:sanctum. The /api/tasks example in this part does not use that middleware, so treat it as a public endpoint until you add authentication in a later article. Do not assume the API is locked just because Sanctum is installed.
JSON response: return Task::all()
In a web controller, return view('tasks.index', ...) builds an HTML page. In an API controller, you return the data itself. Laravel sees an Eloquent collection or model and turns it into a JSON response automatically — you do not call json_encode() yourself.
// app/Http/Controllers/Api/TaskController.php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Task;
class TaskController extends Controller
{
public function index()
{
return Task::all();
}
}Start the app (composer run dev or php artisan serve), then open http://localhost:8000/api/tasks. You should see the same rows that live in the tasks table, as a JSON array of objects with keys such as id, title, and timestamps. That is the same Tasks data the web app uses, now in a format a mobile app or frontend can parse. More detail on explicit JSON responses is in the Laravel JSON response documentation — for this series, returning the collection is enough.
If the page is empty ([]), you have no rows yet: add a task through the web UI from Part 5 while logged in, then refresh /api/tasks. If you get an HTML error or a missing class, confirm the controller file is under app/Http/Controllers/Api/ and that routes/api.php imports App\Http\Controllers\Api\TaskController.
API Resource: a separate controller and Route::apiResource
A single index route is a start, but a REST API usually needs the rest of CRUD: create a task, show one task, update it, and delete it. Laravel can register those routes for you with apiResource. First, generate a dedicated API controller so you do not overwrite the web TaskController from Parts 2–5:
php artisan make:controller Api/TaskController --apiThe --api flag scaffolds the five API actions (index, store, show, update, destroy) and skips create and edit, which exist only to return HTML forms. Fill in index with return Task::all(); as above, then register the resource in routes/api.php (you can replace the single Route::get once this is in place):
// routes/api.php
use App\Http\Controllers\Api\TaskController;
Route::apiResource(
'tasks',
TaskController::class
);That one call registers the REST routes below. The /api prefix is still added automatically. The official list is in the Laravel API resource controller documentation:
| HTTP method | URI | Action | Purpose |
|---|---|---|---|
| GET | /api/tasks | index | List all tasks as JSON |
| POST | /api/tasks | store | Create a task |
| GET | /api/tasks/{task} | show | Show one task |
| PUT / PATCH | /api/tasks/{task} | update | Update a task |
| DELETE | /api/tasks/{task} | destroy | Delete a task |
What this does not add:
- No
createoreditroutes. Those would return HTML forms (/tasks/create,/tasks/{task}/edit). An API does not serve those pages, which is whyapiResourceleaves them out. - The web
TaskControlleris untouched. Browser users still use the Blade CRUD and auth middleware from Parts 5–6. The API controller is a second class in theApinamespace. - The API is not automatically authenticated.
apiResourceonly declares the routes. Add Sanctum (or similar) when you want clients to prove who they are — that is a later article, not this part.
After you fill in store, show, update, and destroy (the same Eloquent work as Part 5, returning models or a success response instead of redirects), you can confirm the list with php artisan route:list --path=api. For this video and article, getting GET /api/tasks to return JSON from index is the finish line; the other four methods follow the same pattern.
Recap: Laravel tutorial for beginners, Part 7
- API routes are for data, not HTML pages. Run
php artisan install:api. Laravel createsroutes/api.php, separate fromweb.php, and prefixes every route with/api. Your existing web app stays as it is. Open/api/tasksand you get data. - A JSON response is what a mobile app can read. In the API controller,
return Task::all();is enough. Laravel builds the JSON for you from the sameTaskmodel the web app uses. apiResourcegives you the REST CRUD routes. Generate a separateApi/TaskControllerwith--api, thenRoute::apiResource('tasks', TaskController::class). Laravel adds index, store, show, update, and destroy — not create or edit.
If you can open http://localhost:8000/api/tasks and see your tasks as JSON, you have finished Part 7.
What’s next: Part 8 — Deploy
You now have a web app and a JSON API on the same Tasks data. In Part 8 of this Laravel tutorial for beginners, we put that app online:
- Prepare the app for production so it can run on a real host, not only
localhost - Deploy so other people can open the site and the API
Part 8 is coming next, so follow Coding Solutions by Usman on YouTube, Instagram, or TikTok so you do not miss it. Missed a step? Read Part 6: Authentication, Starter Kit & Middleware, or start the series from Part 1.
Need help building a Laravel application?
If this laravel tutorial for beginners has you planning a production Laravel app — a new build that needs a JSON API in front of the same data as your Blade UI, or a mobile client talking to Laravel 12 — I take on selected Laravel development projects. Still choosing a stack? Read Laravel vs Node.js for SaaS, then send a short brief through the contact form and I will tell you whether I am a fit.
FAQ
Q1. What is a Laravel REST API? A. A Laravel REST API is a set of HTTP routes that return data (usually JSON) instead of HTML pages. Clients such as a mobile app or a separate frontend call URLs like GET /api/tasks, POST /api/tasks, and DELETE /api/tasks/{task}. In this series, the API reads the same Task model as the Blade web app.
Q2. What does php artisan install:api do? A. In Laravel 12, php artisan install:api creates the routes/api.php file and registers it with an automatic /api prefix, so a route defined as /tasks is reached at /api/tasks. It also installs Laravel Sanctum for later token authentication. Your existing routes/web.php app is left in place.
Q3. How do I return JSON from a Laravel controller? A. Return an Eloquent model or collection from the controller method, for example return Task::all(); in index. Laravel converts that value to a JSON response automatically. You do not need json_encode(). You can also return response()->json($data) when you want to set a custom status or wrap the payload.
Q4. What is the difference between Route::resource and Route::apiResource? A. Route::resource registers seven routes, including create and edit, which return HTML forms. Route::apiResource registers five routes: index, store, show, update, and destroy. It skips create and edit because an API does not serve those pages. Use resource in web.php and apiResource in api.php.
Q5. Should I reuse the web TaskController for the API? A. No. Keep the web TaskController from Parts 2–5 for Blade views, redirects, and form validation. Generate a separate controller with php artisan make:controller Api/TaskController –api. That way a change to the API does not break the web UI, and the two classes can return different types (a view versus JSON).
Q6. Is the Laravel API secure after install:api? A. Not automatically. install:api installs Laravel Sanctum and may add a sample /user route behind auth:sanctum, but the Task::all() routes in this part are public unless you attach authentication middleware yourself. Do not ship a production API that lists or changes tasks without auth and ownership checks.
Q7. Why doesn’t apiResource add create or edit routes? A. Create and edit exist to show HTML forms in a browser (/tasks/create and /tasks/{task}/edit). A REST API does not return those pages. Clients send POST, PUT, PATCH, or DELETE directly. That is why php artisan make:controller … –api also skips the create and edit methods.
Q8. Can I add an API to the Tasks app I already built in Parts 1–6? A. Yes. Stay in the same Laravel 12 project. Run php artisan install:api, generate Api/TaskController, return Task::all() from index, and register Route::apiResource(‘tasks’, TaskController::class) in routes/api.php. The Blade CRUD and auth middleware from Parts 5–6 keep working on the web routes.
