Skip to content

JamerSC/laravel-inventory-rest-api

Repository files navigation

Inventory API (CRUD) - August 26, 2025 ✅

  1. Create Github/Gitlab repo ✅

  2. Go to .env file ✅

  • Check the database name (update db name optional)
  • Change LOG_CHANNEL=stack to LOG_CHANNEL=daily
  • To locate logs: app > storage > logs >
  1. Go to browser type http://localhost/phpmyadmin/ (or SQLyog) crate database: ex. invetory_api_db ✅

  2. 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
  1. Controller versioning ✅
  • Create folders: Go to app > http > Controllers > create folder name Api then inside create sub folder name V1
  • 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
  1. 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
  1. 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
  1. 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
  1. 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
  1. 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

Laravel Command

  • 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

About

Inventory Rest API with Laravel - PHP Framework

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published