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:
So, Lets get started
Step 1:
Create the model using the command
php artisan make:model Book -m
then it will create the models named as Book.php in Models directory. This is for Laravel 8 only for below version models is created on App directory without sub folder Models.
database/migrations/......._create_books_table.php
Modify the up() function for your custom field.
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('ISBN')->unique();
$table->string('author');
$table->biginteger('quantity');
$table->longText('title');
$table->string('image_url');
$table->tinyInteger('active')->default(1); //1=>active
$table->timestamps();
$table->softDeletes();
});
}
then run migration command
php artisan serve
Step 2:
Now we will create a controller which will hold all the methods for the API. Run the following command to create a controller.
php artisan make:controller BookController
Now write the logic for CRUD Operation
1. function to get all the Books
public function getBooks()
{
$books = Book::get();
return response()->json([
'Books' => $books
]);
}
2. function to get single book
public function getSingle($id)
{
$book = Book::find($id);
return response()->json([
'Book' => $book
]);
}
3. function to store or create the books
public function create(Request $request)
{
$books = new Book();
$request->validate([
'title'=>'required|string',
'ISBN'=>'required|string',
'author'=>'required|string',
'quantity'=>'required|integer',
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
if ($request->hasFile('image')) {
$imageName = time().'.'.request()->image->getClientOriginalExtension();
request()->image->move(public_path('books'), $imageName);
$books->image_url=$imageName;
$isbn = $request->ISBN;
$isbn='00'.$books->id.time();
$books->ISBN = $isbn;
$books->title = $request->title;
$books->author=$request->author;
$books->quantity= $request->quantity;
$books->save();
return response()->json([
"success" => true,
"message" => "Book added successfully!!",
"file" => $imageName
]);
}
}
4. function to update the books
public function update(Request $request, $id) {
$book = Book::find($id);
$book->title = is_null($request->title) ? $book->title : $book->title;
$book->author = is_null($request->author) ? $book->author : $book->author;
$book->quantity = is_null($request->quantity)? $book->quantity : $book->quantity;
if( $book->save()){
return response()->json([
"message" => "records updated successfully"
], 200);
}
else{
return response()->json([
"message" => "Book not found"
], 404);
}
}
5. function to delete the books
Step 3
The routes
Add the routes to the API routes file, to access all the functions we wrote.
routes/api.php
Route::group(['prefix'=> 'libarian'], function()
{
Route::get('books',[App\Http\Controllers\BookController::class,'getBooks']);
Route::get('books/{id}',[App\Http\Controllers\BookController::class,'getSingle']);
Route::delete('delete_books/{id}',[App\Http\Controllers\BookController::class,'destroy']);
Route::put('update_books/{id}',[App\Http\Controllers\BookController::class,'update']);
Route::post('add_books',[App\Http\Controllers\BookController::class,'create']);
}
Now we can go to test phase in Postman