-
Create Github/Gitlab repo ✅
-
Go to
.env
file ✅
- Check the database name (update db name optional)
- Change
LOG_CHANNEL=stack
toLOG_CHANNEL=daily
- To locate logs: app > storage > logs >
-
Go to browser type http://localhost/phpmyadmin/ (or SQLyog) crate database: ex. invetory_api_db ✅
-
Create Product Model Class ✅
- Create a product model: terminal type
php artisan make:model Product --all
- Update & add column/fields:
Go to inventory-api > database > migrations > 2025_08_26_023222_create_products_table.php
- Generate fake eloquent model instances :
Go to inventory-api > database > factories > ProductFactory.php
- Populate your database with initial data or test data (import the Product class model):
Go to inventory-api > database > seeders > ProductSeeder.php
- Next call the Product seeder
Go to inventory-api > database > seeders > DatabaseSeeder > run function add the code
->$this->call(ProductSeeder::class);
- Then reset your database and then populate it dummy data.
Open the terminal & type the command
->php artisan migrate:fresh --seed
- Controller versioning ✅
- Create folders:
Go to app > http > Controllers >
create folder nameApi
then inside create sub folder nameV1
- Move the
ProductContoller
to V1 folder - Update the ProductController namespace:
App\Http\Controllers\Api\V1;
- Update import the base controller:
use App\Http\Controllers\Controller;
- ProductController index() function add new line of code to fetch all the Producs:
return Product::all();
- Update show() function add the line of code to view a specific product:
return $product
- Routes Setting (Display all the products) ✅
- Create a route:
Go to inventory-api > routes > api.php
then open the file then add routes - After adding the new route. Open the terminal and type the command:
php artisan optimize
to take effect and use the changes espcially in routes - Next run the server:
php artisan serve
- Start getting request. Open a browser or postman (use get method) then paste the api url:
http://127.0.0.1:8000/api/v1/products
can view all the products
- Transform Database into JSON with Resource (Database to JSON format) ✅
- Create a product resource:
php artisan make:resource V1\ProductResource
- Created Resource Located at the
app > http > resources > v1 > ProductResource
- Update the data that will be display in Json response:
return ["name"=> $this->name];
- Update the ProductController >
show() method
return new ProductResource - Create a product resource:
php artisan make:resource V1\ProducCollection
- Created Resource Located at the
app > http > resources > v1 > ProductCollection
- Stay the ProductCollection as default
- Update the ProductController >
index() method
return new ProductCollection
- Create a Product using POST Request
- Create StoreProductRequest:
php artisan make:request V1\StoreProductRequest
- Located at `app > http > requests > StoreProductRequest
- Import to ProductController
- Update ProductController >
store() { return new ProductResource(Product::create($request->all())); }
- Run the server & test in the postman using post request method
- Create a Product PUT & PATCH request
- Create UpdateProductRequest:
php artisan make:request V1\UpdateProductRequest
- Located at `app > http > requests > UpdateProductRequest
- Update ProductController >
update() { $product->update($request->all());}
- Run the server & test in the postman using put & patch request method
- Delete a Product DELETE request
- Update ProductController >
destroy() { $product->delete(); return response()->json([],200); }
- Run the server & test in the postman using put & patch request method
- check laravel version
php artisan --version
- Run the server:
php artisan serve
- Any changes in the routes and config optimize it using:
php artisan optimize
- Refresh/truncate database tables:
php artisan migrate:fresh
- Populate test data:
php artisan migrate:fresh --seed
- Create a resource:
php artisan make:resource V1\ProductResource