Assuming you have an existing Laravel project, we will create a database for Books and we will write endpoints for its CRUD operations, which means Create, Read, Update and Delete. We will create the following endpoints:
GET /api/books : to return a list of all the books
GET /api/books/{id} : to return details of a particular book by ID
POST /api/books : to create a new book
PUT /api/books/{id} : to update details of a book by iD
DELETE /api/books/{id} : to delete a book by ID
So let’s get started.
Note: If you don’t have an existing Laravel project, please check this and this.
Step 1
Create the model
First we create the model of the book.
php artisan make:model Book -m
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Book extends Model { protected $fillable = [ 'name', 'author' ]; }
... public function up() { Schema::create('books', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('author'); $table->timestamps(); }); } ...
php artisan migrate
Step 2
php artisan make:controller Api/BookController
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Book; class BookController extends Controller { }
Now write functions logic for each of the endpoints.
1. Function to get all the books from the database table.
public function getAllBooks() { $books = Book::get()->toJson(JSON_PRETTY_PRINT); return response($books, 200); }
2. Function to get a particular book by ID
public function getBook($id) { if (Book::where('id', $id)->exists()) { $book = Book::where('id', $id)->get()->toJson(JSON_PRETTY_PRINT); return response($book, 200); } else { return response()->json([ "message" => "Book not found" ], 404); } }
3. Function to create a new book
public function createBook(Request $request) { $book = new Book; $book->name = $request->name; $book->author = $request->author; $book->save(); return response()->json([ "message" => "Book record created" ], 201); }
4. Function to update a book by ID
public function updateBook(Request $request, $id) { if (Book::where('id', $id)->exists()) { $book = Book::find($id); $book->name = is_null($request->name) ? $book->name : $book->name; $book->author = is_null($request->author) ? $book->author : $book->author; $book->save(); return response()->json([ "message" => "records updated successfully" ], 200); } else { return response()->json([ "message" => "Book not found" ], 404); } }
5. Function to delete a book by ID
public function deleteBook ($id) { if(Book::where('id', $id)->exists()) { $book = Book::find($id); $book->delete(); return response()->json([ "message" => "records deleted" ], 202); } else { return response()->json([ "message" => "Book not found" ], 404); } }
Now that we have written all the logic, let us define routes for these.
Step 3
Route::get('books', 'Api\BookController@getAllBooks'); Route::get('books/{id}', 'Api\BookController@getBook'); Route::post('books', 'Api\BookController@createBook'); Route::put('books/{id}', 'Api\BookController@updateBook'); Route::delete('books/{id}','Api\BookController@deleteBook');
Test the endpoints


Step 4
Route::group(['middleware' => 'auth:api'], function() { //routes here });

Leave A Comment