Breaking Posts

6/trending/recent
Type Here to Get Search Results !

create api in laravel simple steps




Create a new Laravel project using composer:

lua

Copy code

composer create-project --prefer-dist laravel/laravel api-project

Create a new controller for your API using the artisan command:

go

Copy code

php artisan make:controller ApiController

In your ApiController class, define a method that will handle the API request. For example, you can create a getUsers method that will return a list of users in JSON format:

php

Copy code

namespace App\Http\Controllers;


use App\Models\User;

use Illuminate\Http\Request;


class ApiController extends Controller

{

    public function getUsers()

    {

        $users = User::all();

        return response()->json($users);

    }

}

Define a route for your API in routes/api.php:

php

Copy code

Route::get('/users', 'ApiController@getUsers');

Start your Laravel development server:

Copy code

php artisan serve

Send a GET request to your API endpoint using a tool like cURL or Postman:

bash

Copy code

curl http://localhost:8000/api/users

You should receive a JSON response containing a list of users. This is just a basic example, but you can use Laravel's built-in features like middleware, authentication, and validation to create more complex APIs. 

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.