Courses

How to Build Laravel 10 API From Scratch

List of Data: Routing, Controllers and Clients Demo

Welcome to this course about creating APIs in Laravel.

Let's dive into practice right away and start with a very simple example of building API by creating an endpoint for the categories list.


We have a new Laravel project, so first, we will create a new Model with Migration and Factory.

php artisan make:model Category -mf

database/migrations/xxx_create_categories_table.php:

public function up(): void
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}

app/Models/Category.php:

class Category extends Model
{
use HasFactory;
 
protected $fillable = ['name'];
}

database/factories/CategoryFactory.php:

class CategoryFactory extends Factory
{
public function definition(): array
{
return [
'name' => fake()->words(asText: true),
];
}
}

database/seeders/DatabaseSeeder.php:

use App\Models\Category;
 
class DatabaseSeeder extends Seeder
{
public function run(): void
{
Category::factory(10)->create();
}
}

And run the migrations with the seed.

php artisan migrate --seed

Now, let's create the first API route endpoint to list all the categories. Let's start from the route.

routes/api.php:

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
 
Route::get('categories', [\App\Http\Controllers\Api\CategoryController::class, 'index']);

And let's create a Controller.

php artisan make:controller Api/CategoryController

In the Controller, we will return categories. By default, Laravel will take care to return JSON.

app/Http/Controllers/Api/CategoryController.php:

use App\Models\Category;
use App\Http\Controllers\Controller;
 
class CategoryController extends Controller
{
public function index()
{
return Category::all();
}
}

How do we launch the API from the browser or any client? Since this API route is public, you can launch it in the browser or any client.

First, let's see it in the browser. But before that, important part here. You would think that the route is /categories, but in fact, because it is in the routes/api.php, all routes have a prefix of /api. This setting is set in the RouteServiceProvider.

app/Providers/RouteServiceProvider.php:

class RouteServiceProvider extends ServiceProvider
{
// ...
 
public function boot(): void
{
$this->configureRateLimiting();
 
$this->routes(function () {
Route::middleware('api')
->prefix('api')
->group(base_path('routes/api.php'));
 
Route::middleware('web')
->group(base_path('routes/web.php'));
});
}
 
// ...
}

As you can see, the prefix and filenames for the routes are set here.

After visiting the /api/categories endpoint in the browser, we see the result in a JSON format.

But here is where I advise to use any API client of your choice. They format results more readably and provide more features like authenticating users.

Here is an example screenshot from the Postman and Insomnia clients.


This is the first step. It is easy to create an API endpoint and return the data if you don't need any more logic.

avatar

thank you povilas, please make more practical examples to get people going by doing. its the easiest way to start practising and learning new skills

👍 1