Let's start by creating our base API application with our first CRUD - Categories:
Creating a new Laravel Project
Let's start by creating a new Laravel project. Open your terminal and run the following command:
laravel new laravel-api-flutter-api-code
Then we will select None
as our Starter kit:
Creating our First Model and Migration
Let's create our first model and migration for our categories. Run the following command:
php artisan make:model Category -m
Then we fill in our fields:
Migration
Schema::create('categories', function (Blueprint $table) { $table->id(); $table->foreignId('user_id')->nullable()->constrained(); $table->string('name'); $table->timestamps();});
Then, we prepare our model:
app/Models/Category.php
use Illuminate\Database\Eloquent\Factories\HasFactory;use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\Relations\BelongsTo; class Category extends Model{ use HasFactory; protected $fillable = [ 'user_id', 'name', ]; public function user(): BelongsTo { return $this->belongsTo(User::class); }}
Creating a Controller
Let's create our API Resource Controller for our categories. Run the following command:
php artisan make:controller Api/CategoryController --resource --api --model=Category
This will create a new controller with all the necessary methods for our CRUD:
app/Http/Controllers/Api/CategoryController.php
use App\Http\Controllers\Controller;use App\Models\Category;use Illuminate\Http\Request; class CategoryController extends Controller{ /** * Display a listing of the resource. */ public function index() { // } /** * Store a newly created resource in storage. */ public function store(Request $request) { // } /** * Display the specified resource. */ public function show(Category $category) { // } /** * Update the specified resource in storage. */ public function update(Request $request, Category $category) { // } /** * Remove the specified resource from storage. */ public function destroy(Category $category) { // }}
Returning First API Response
To keep this simple, we will add the most straightforward response to our index method:
app/Http/Controllers/Api/CategoryController.php
use App\Models\Category; // ... public function index(){ return Category::all();}
Thanks to Laravel magic, this will return a JSON response with all our categories.
Installing Sanctum
Laravel 12 comes with a lovely helper command to install API scaffolding:
php artisan install:api
This will install Sanctum and set up the necessary configuration for it:
With this completed, we should see a few things added:
-
config/sanctum.php
file -
bootstrap/app.php
file modified - migration for
personal_access_tokens
table added -
routes/api.php
file created
Registering our Route
Now we can register our Route in the api.php
file:
routes/api.php
use App\Http\Controllers\Api\CategoryController; // ... Route::apiResource('categories', CategoryController::class);
Creating a Factory
As a last step before testing our API Endpoint - we need to create a factory for our categories:
php artisan make:factory CategoryFactory
database/factories/CategoryFactory.php
use App\Models\Category;use App\Models\User;use Illuminate\Database\Eloquent\Factories\Factory; /** * @extends Factory<Category> */class CategoryFactory extends Factory{ /** * Define the model's default state. * * @return array<string, mixed> */ public function definition(): array { return [ 'name' => fake()->name, 'user_id' => User::factory() ]; }}
And, of course, we need to register this to our Seeder:
database/seeders/DatabaseSeeder.php
use App\Models\Category;use App\Models\User;use Illuminate\Database\Seeder; // use Illuminate\Database\Console\Seeds\WithoutModelEvents; class DatabaseSeeder extends Seeder{ /** * Seed the application's database. */ public function run(): void { // User::factory(10)->create(); User::factory()->create([ 'name' => 'Test User', 'email' => 'test@example.com', ]); Category::factory()->count(5)->create(); }}
Now we can run our migrations and seed our database:
Note: We are doing a fresh migration to completely reset the database.
php artisan migrate:fresh --seed
Looking at the database, we can see that our categories have been created:
Testing our API
We will use Postman to test out the API:
Note: If you are wondering why we have set the headers you can read more about it here.
That's it! We have created a basic working API endpoint. But it's not up to the standards yet. We will improve it in the next lesson.
Check out the GitHub Commit for this lesson and API GitHub Commit to see the final code.
Hi Povilas, We need react native with laravel API as well
We'll think about it and I will ask the audience whether there's a real demand :)