Tuesday 18 June 2024

Setting up Migrations for a Laravel 11 API Model

Migrations in Laravel are a way to change the schema of a database in an organized and version-controlled manner. They provide a way to easily share the schema of the database and make modifications to it. Migrations are like creating a schema once and then sharing it many times.

Pre-requisites

We'll assume that the model was created using the approach in this blog post.

When the model was created in the pre-requisite, a migration file was also created for our model in the directory database/migrations.

It is in this file that we set the field types which will be populated as records are used within CRUD operations and listings. Below are examples of how these should be set in order to facilitate this:

public function up(): void

{

Schema::create('yourmodel', function (Blueprint $table) {

    $table->id();

    $table->string('name');

    $table->text('description')->nullable();

    $table->timestamps();

});

}

public function down(): void

{

Schema::dropIfExists('yourmodel');

}

It would be possible to run the migrations as is though:

php artisan migrate

but we may be best served, by seeding these migrations as we run them in order to have some test data.

 

Create Routes for a Laravel 11 API Model

Laravel routes are used to bind URLs to controller actions. REST methods are often used to describe the type of HTTP action which the route will respond to. 

Pre-requisites

Check to see if you already have a routes/api.php for this model.
This should have been created by running:
php artisan install:api

Now create a route for our class:
Route::apiResource('/yourmodelname', YourController::class);
Now in the terminal:
php artisan route:list --path=api
This creates a list of all the endpoints. You may wish to make a copy of these.

Before we can test the endpoints, we should add a response in our controller. I like to add
return response()->json(['message' => 'Hello World!']);
to the public function index().

We should now be in a position to test our endpoints.
We should run the server as:
php artisan serve
Then use a tool like Postman to test the first endpoint.
Commonly, when working locally, this would take the form of a GET request to and endpoint of:
http://localhost:8000/api/yourcontrollername
We should now see some JSON showing the message "Hello world".

Create a controller for a Laravel 11 API Model

Pre-requisites

Check to see if you already have a controller for this model. 

When the model was created using the approach in this blog post, a controller was created.

By running:
php artisan make:controller
you are provided with a list of prompts:
  1. You're asked to give it a name. I like to give it the same name as the model I just created.
  2. You're asked to select the type of controller to create. For this example, I like to select API.
  3. You're asked to select the model that this API controller would be used for. Here I name the model I just created.

Create a Laravel 11 API Model

Basic Steps

php artisan make:model
Give it a name
You then have a bunch of options for your model. A good place to start, would be to select.
  • Database Seeder
  • Factory
  • Form Requests
  • Migration
  • Resource Controller
Once run, the system gives you a path list of the files created by this process.
You would then be in a good place to create routes for your API. 

Create your Laravel 11 app as API only

Pre-requisites

I'll assume that you have Laravel set up to use laravel commands.

Make sure you have SQLite installed.

Basic Steps

First, create your application:

laravel new basic-api

Apply the API

php artisan install:api

Delete the resources directory

Delete the file routes/web.php

Delete the line web: __DIR__.'/../routes/web.php', from bootstrap/app.php

Thursday 13 June 2024

Create a basic API using Laravel 11

This post is specific to Laravel 11, which has a sightly different way of setting up it's API to previous versions.

Pre-requisites

I'll assume that you have Laravel set up to use laravel commands.

Make sure you have SQLite installed.

Basic Steps

Run:

laravel new basic-test

To question 'Would you like to install a starter kit?', select 'No starter kit'.

To question 'Which testing framework do you prefer?',  select 'Pest'.

To question 'Would you like to initialize a Git repository?', select 'yes'.

To question 'Which database will your application use?', select 'SQLite'.

To question 'Would you like to run the default database migrations?', select 'yes'.

To question 'Would you like to create it?', select 'yes'.

Run:

cd basic-test

Run:

php artisan install:api

Within routes/api.php, add the following lines below the 'use' statements:

Route::get('/', function () {

    return response()->json(['message' => 'Hello World']);

});

Run:

php artisan serve

Open a web browser, and add http://127.0.0.1:8000/api/ to the address bar.

You should see some JSON showing the message 'Hello world'.