Now its time to learn how to develop an api using Laravel . Let us start building our first api .
Figure 1: The following image represents the .env file in the Laravel project
You must edit the above based on your database settings where DB_DATABASE is your database name , DB_USERNAME is the username of you database which is in my case root and DB_PASSWORD is the password of the database which is in my case root
The Model represents the application's data and business logic. It is responsible for managing and storing data, as well as defining the rules and behaviors that govern how the data is processed and manipulated.
For example, if you want to get data from the users
table in your database you must create a model named as User
(be careful the model must be named singular )
Note : You can't create two models of the same name so if a model already exists no need to create it again
You can create a model by writing the following script in your terminal
php artisan make:model User
The Controller acts as an intermediary between the Model and the View. It receives user input from the View and processes it, making decisions and updates to the Model as needed. It controls the flow of data between the Model and the View, handling user actions and managing the application's behavior.
Lets do the following steps to create a function that get all users :
UsersController
by running the following script in your terminalphp artisan make:Controller UsersController
getAllUsers()
is the same as writing a queryselect * from users
Figure 2 : Represents the UsersController
Notes:
users
Figure 3 : Represents the api function in the api.php
file
Run the following script in your terminal and click on the link to check your results
php artisan serve
Positive Note : You are doing a great work keep it up
Previous:/public/api-beginners/api View Next:/public/api-beginners/artisan-commands