How to create API in Laravel

With these steps, you have created a basic API in Laravel that returns a list of all books stored in the database. You can extend it by adding more routes, methods, and resources to meet your specific needs.

How to create API in Laravel

Here is an example of how you can create an API in Laravel and the expected output of an API call.

  1. Install Laravel: To install Laravel, you can run the following command in your terminal:

    PHP
    composer create-project --prefer-dist laravel/laravel project_name
  2. Create a Model and Migration: In Laravel, models are used to interact with the database, and migrations are used to manage the schema of the database. To create a model and a migration for a Book model, you can run the following command in your terminal:

    PHP
    php artisan make:model Book -m
  3. Define the Schema: Open the migration file located at database/migrations/[timestamp]_create_books_table.php, and define the schema for the books table. For example:

    PHP
    public function up()
    {
        Schema::create('books', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('author');
            $table->integer('pages');
            $table->timestamps();
        });
    }
  4. Run the Migration: To create the books table in the database, you can run the following command in your terminal:

    PHP
    php artisan migrate
  5. Create a Resource: In Laravel, a resource is used to transform a model into a JSON response. To create a resource for the `Book` model, you can run the following command in your terminal:

    PHP
    php artisan make:resource BookResource
  6. Define the Route: In Laravel, routes are used to map URLs to controllers. To define a route for getting a list of all books, you can open the `routes/api.php` file and add the following code:

    PHP
    Route::get('books', 'BookController@index');
  7. Create a Controller: In Laravel, controllers are used to handle HTTP requests. To create a controller for the `Book` model, you can run the following command in your terminal:

    PHP
    php artisan make:controller BookController
  8. Implement the Method: Open the controller file located at `app/Http/Controllers/BookController.php`, and implement the index method that was defined in the route. For example:

    PHP
    public function index()
    {
        return BookResource::collection(Book::all());
    }
  9. Test the API: To test the API, you can use a tool like Postman to make a GET request to the URL `http://localhost:8000/api/books`. The expected output would be a JSON array of books, where each book is represented as a JSON object with the properties `id`, `title`, `author`, `pages`, `created_at`, and `updated_at`.

With these steps, you have created a basic API in Laravel that returns a list of all books stored in the database. You can extend it by adding more routes, methods, and resources to meet your specific needs.

What's Your Reaction?

like
0
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0