Book Store API Application Using Laravel 8 CRUD Operation

In this article, we will implement a Laravel 8 CRUD
book store application. After reading this article, you will:
- Learn how to create tables in
MySQL
database withOne-to-Many
andMany-to-Many
relationships. - Implement the models and the functions that they serve.
- Know how to implement CRUD with Laravel Resouce Controller
- Get familiar with
belongsTo
andbelongsToMany
methods
A simple definition of the project
Every book belongs to one publisher, so you will be able to assign your book to a specific publisher or if you can’t find it you can store a new publisher in the database.
Besides that, you can store authors in the authors’ table and include one of them or multiple of them in your book as the author’s name. If you don’t want to read this whole text you can skip it and find the whole code in the repository which I insert the link at the end of the article.
Let’s take a look at how our database is designed, then we’ll dive into the code.

One-to-Many Relationship
There is a one-to-many relationship between books and publishers. An element of publishers may be linked to many elements of books, but a member of books is linked to only one element of publishers.
Many-to-Many Relationship
There is a many-to-many relationship between books and authors.
Every book has belonged to many authors and every author has written many books. So in this situation, we will use a table as a pivot table to store relations. Our pivot table is book_authors.
Create Tables in Database
After creating the project with the composer, It’s time to create our tables and migrate them to the database. In Laravel, we’ll use the below command to create the table.php artisan make:migration create_publishers_table
First of all, we’ll create the publishers’ table that will have the below structure
And here is the rest of the tables that you should create in order.
Create Models
In this section, we will talk about the models and the functions that they serve. First, we’ll create the Publisher model with php artisan make:model Publisher
command and as you see in the database structure there is a one-to-many relationship between books and publishers, and this is defined by defining a method in the Eloquent model.
It’s time to create other models and define the many-to-many relationship between books and authors. so we’ll create the BookAuthor model as a pivot Eloquent. To do this make sure that your model extends Illuminate\Database\Eloquent\Relations\Pivot
And now we’ll create the Author model which will contain the function with books name that uses the belongsToMany
method and BookAuthor
model as a Pivot
model to get the list of all books of the author object.
Finally, we’ll have the book model that uses the belongsTo
method to get the related publisher in the function with the publisher name. and uses the belongsToMany
method and BookAuthor
model as a pivot model to get the list of all authors of the book object.
Create Controllers
In this project every controller will contain the following functions:
- The index function will be used to get a full list of all records.
- In the store function after validating the requests, the new element will be created.
- The show function is used to get the specific element by passing ID to the function.
- We’re passing the ID to the destroy function and then detecting elements with the given ID and then deleting it.
- The update function is used to update the given element based on the given requests.
In the BookController store and update function, you will see the $item->authors()->sync($requeest->get('authors'))
code. sync
can be used to synchronize either side of a belongsToMany
relationship. By default, it drops all the relations and just keeps those provided as the arguments. Similarly, if you want to remove a certain entity relationship from the pivot table, you can use the method. For instance, if you want to remove authors from a book you can do this with $item->authors()->detach()
code.
Create Routes
Finally, we’ll define our routes in the routes/api.php
directory.Using Route:apiResource
sets up some default routes. And you can see the list of all routes by running the route:list
Artisan command.
Now you have a book-store API. The full code is in the below repository.
I hope this course will help you become familiar with database design, one-to-many relationships, and many-to-many relationships in Laravel.