Often I used Laravel API for my backend project. I work with front end developers using React to generate model data through an API. I chose Laravel because its secured and adaptable to many enpoints and comes with security and authentication.. This is going to be a fresh install of Laravel 10.
This article includes step 1 to install Laravel with users, Step 2 (link below) will add user authentication with tokens for SPA ready!
1. Create a Laravel Project:
$ laravel new [APP-NAME]
| | | |
| | __ _ _ __ __ ___ _____| |
| | / _` | '__/ _` \ \ / / _ \ |
| |___| (_| | | | (_| |\ V / __/ |
|______\__,_|_| \__,_| \_/ \___|_|
Creating a "laravel/laravel" project at "./noprem-laravel"
Installing laravel/laravel (v10.2.9)
2. Cd into the new app
$ cd [APP-NAME]
Add Sanctum, Resoures:
$ composer require laravel/sanctum
$ php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
$ php artisan migrate
$ code .
$ code README.md
$ code app/Http/Kernel.php
$ git init
$ code .gitignore
- > and Add README.me to make your notes.$ git add
.$ git commit -m "initial"
Make a list of all the user form fields you will be needing: For example:
View a list of all available Helpers: https://laravel.com/docs/10.x/helpers
Factories: Use this if you need to generate alot of fake users. If you need just one, go to the next step: Seeders For Factories, See below for more details
$ code database/factories/UserFactory.php
Example:
Seeders - You can use the factory you created in the previous step to generate fake users, open DatabaseSeeder.php and uncomment the factory or you can create individual entries. Add the fields you need for a new user creation.
$ code database/seeders/DatabaseSeeder.php
Example:
Models - Open the Models/User.php file to add the fields
$ code Models/User.php
Resource:
Example:
Migrations database schema definition -
$ code database/migrations/*_create_users_table.php
Column Types: https://laravel.com/docs/10.x/migrations#available-column-types
Example:
Create Datatabse to connect and update .evn file:
$ code .env
Build Database
$ php artisan migrate:refresh --seed
Resource: https://www.webune.com/forums/laravel-database-migration-notes-sheetcheat-migrate-explanation.html
Start the server
$ php artisan serve
You can use postman to test API endpoints:
Factories allow you to generate fake users, however many you want
$ code database/factories/UserFactory.php
Reference: https://laravel.com/docs/10.x/eloquent-factories#generating-factories
UserFactory has alread been created in database/factories/UserFactory.php by default.
You can add one fake user or use the Factory, for example:
Optional: Email Verification:
https://laravel.com/docs/10.x/verification