Code | Solutions

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:

  1. API routes — php artisan install:api creates routes/api.php, separate from web.php, and prefixes every route with /api.
  2. JSON response — return Task::all() from the API controller; Laravel automatically builds JSON that a mobile app or frontend can parse.
  3. API Resource — a separate Api/TaskController and Route::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:api

That 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/tasks

What this changes:

  • The path you type in the browser is /api/tasks, not /tasks. Laravel adds the /api prefix for you. You do not write /api in the Route::get path.
  • /tasks in the browser is still the Blade list from Parts 3–5, behind the auth middleware from Part 6. The API is a second door to the same Task model.
  • 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 --api

The --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 methodURIActionPurpose
GET/api/tasksindexList all tasks as JSON
POST/api/tasksstoreCreate a task
GET/api/tasks/{task}showShow one task
PUT / PATCH/api/tasks/{task}updateUpdate a task
DELETE/api/tasks/{task}destroyDelete a task

What this does not add:

  • No create or edit routes. Those would return HTML forms (/tasks/create, /tasks/{task}/edit). An API does not serve those pages, which is why apiResource leaves them out.
  • The web TaskController is untouched. Browser users still use the Blade CRUD and auth middleware from Parts 5–6. The API controller is a second class in the Api namespace.
  • The API is not automatically authenticated. apiResource only 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

  1. API routes are for data, not HTML pages. Run php artisan install:api. Laravel creates routes/api.php, separate from web.php, and prefixes every route with /api. Your existing web app stays as it is. Open /api/tasks and you get data.
  2. 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 same Task model the web app uses.
  3. apiResource gives you the REST CRUD routes. Generate a separate Api/TaskController with --api, then Route::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.


Tags

2026 ai tools aiagent Angular 21 Angular Developer for Hire Angular Performance Angular Signals Angular TypeScript Best Practices API Authentication API Development API Integration API Security API Versioning B2B SaaS Backend Development Backend Framework Comparison Best PHP Framework 2026 Best Practices Box Model CakePHP CakePHP 2026 CakePHP Laravel PHP Developer CakePHP Tutorial 2026 CakePHP vs Laravel Case Study Client Management cost to hire freelance developer crewai CRM Integration css css animation CSS Tips CSS Tutorial Database Optimization Developer Tools Enterprise Angular Development Express.js FastAPI Tutorial 2026 framework comparison Freelance Angular Developer Freelance Developer freelance developer guide freelance developer tips Freelance Development freelance full-stack developer Freelance Laravel Developer Freelance PHP Developer Freelance Web Developer freelance web developer process Freelance Web Development Freelancer vs Agency Freelancing Tips Front-End Development frontend Frontend Development Full Stack Developer Full Stack Developer Pakistan Full Stack Development Full-Stack Case Study Full-Stack Development Grid Hire a Developer Hire Angular Developer hire freelance developer hire freelance full-stack developer Hire Laravel Developer how to work with a freelance dev how to write a project brief individual software engineer freelance individual software engineer hire Inertia.js javascript JavaScript Frameworks JWT Authentication laravel Laravel 13 Laravel 2026 Laravel API 2026 Laravel Application Laravel Development Laravel Optimization Scale Laravel Performance Laravel REST API Laravel REST API Tutorial Laravel Sanctum Laravel Tutorial Laravel vs CakePHP Laravel Vue Tutorial multi-agent Next.js Node.js Node.js API Node.js developer Node.js Developer Portfolio Node.js Express Node.js Tutorial Nuxt PERN Stack PHP PHP Developer PHP Developer for Hire PHP Development PHP Framework PHP Framework Comparison PHP Performance PHP Web Development PostgreSQL Prisma Production Deployment project brief template Project Management Python Backend Developer Python FastAPI react React developer for hire React Node.js Developer Portfolio React SPA React Tutorial React vs Vue Real-Time Chat App Redis Caching Responsive Design REST API SaaS Development Sanctum SMB Web Development Socket.io React Real-Time App Socket.io Tutorial Software Development software development process Software Integration software project brief SSG SSR Standalone Components System Integration TypeScript Strict Mode Vite vue Vue 4 Composition API Vue Laravel Developer Vue.js Development web app development process Web Design Web Design Fundamentals web development Web Development 2026 Web Development Cost Web Layout Web Projects WebSocket

Recent News