This is my the code that is contained in my Migration File of Category Table:
<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->foreignId('parent_id')->nullable()->constrained('categories')->onDelete('cascade');
$table->string('name');
$table->string('icon')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('categories');
}
};
This is my the code that is contained in my Model File of Category.php:
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Category extends Model
{
use HasFactory;
protected $fillable = ['parent_id', 'name', 'icon'];
public function scopeParentCategories($query)
{
return $query->whereNull('parent_id');
}
public static function tree()
{
$allCategories = Category::get();
$rootCategories = $allCategories->whereNull('parent_id');
self::buildTree($rootCategories, $allCategories);
return $rootCategories;
}
public function children()
{
return $this->hasMany(Category::class, 'parent_id');
}
public function items()
{
return $this->belongsToMany(Item::class, 'category_items');
}
public function saveSubCategories($subCategories)
{
foreach($subCategories as $subCategory) {
$this->saveSubCategory($subCategory);
}
}
public function saveSubCategory($subCategory)
{
$newSubCategory = new self();
$newSubCategory->fill($subCategory);
$newSubCategory->setAttribute('parent_id', $this->id);
$newSubCategory->save();
}
private static function formatTree($categories, $allCategories)
{
foreach ($categories as $category) {
$category->children = $allCategories->where('parent_id', $category->id)->values();
if ($category->children->isNotEmpty()) {
self::formatTree($category->children, $allCategories);
}
}
}
}
This is my the code that is contained in my Controller File of CategoryController.php:
<?php
namespace AppHttpControllers;
use AppModelsCategory;
use IlluminateHttpRequest;
use IlluminateSupportFacadesValidator;
class CategoryController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
return Category::all();
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|string|max:255',
'icon' => 'nullable|string|max:255',
'children.*.name' => 'required|string|max:255',
'children.*.icon' => 'required|string|max:255',
'children.*.parent_id' => 'nullable|exists:categories,id',
]);
if ($validator->fails()) {
return response()->json(['errors' => $validator->errors()], 400);
}
$category = Category::create($request->only('name', 'icon'));
if ($request->has('children')) {
foreach ($request->children as $childData) {
$child = new Category($childData);
$child->parent_id = $category->id;
$child->save();
}
}
return $category;
}
/**
* Display the specified resource.
*/
public function show(Category $category)
{
return Category::find($category);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Category $category)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Category $category)
{
$category = Category::findOrFail($category);
$validator = Validator::make($request->all(), [
'name' => 'required|string|max:255',
'parent_id' => 'nullable|exists:categories,id',
'icon' => 'nullable|string|max:255',
]);
if ($validator->fails()) {
return response()->json(['errors' => $validator->errors()], 400);
}
$category->update($request->all());
return response()->json($category);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Category $category)
{
$category = Category::findOrFail($category);
$category->delete();
return response()->json($category);
}
}
This is my the code that is contained in my routes File of api.php:
<?php
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
use IlluminateSupportFacadesRoute;
use AppHttpControllersCategoryController;
use AppHttpControllersItemController;
use AppHttpControllersItemOptionController;
use AppHttpControllersOptionController;
use AppHttpControllersOptionValueController;
use AppHttpControllersOrderController;
use AppHttpControllersOrderItemController;
use AppHttpControllersPaymentController;
use AppHttpControllersQRCodeController;
use AppHttpControllersTableLayoutController;
// API version prefix
Route::prefix('v1')->group(function () {
// Routes for managing categories
Route::get('categories', [CategoryController::class, 'index']);
Route::post('categories', [CategoryController::class, 'store']);
Route::get('categories/{category}', [CategoryController::class, 'show']);
Route::put('categories/{category}', [CategoryController::class, 'update']);
Route::delete('categories/{category}', [CategoryController::class, 'destroy']);
// Routes for managing items
Route::get('items', [ItemController::class, 'index']);
Route::post('items', [ItemController::class, 'store']);
Route::get('items/{item}', [ItemController::class, 'show']);
Route::put('items/{item}', [ItemController::class, 'update']);
Route::delete('items/{item}', [ItemController::class, 'destroy']);
// Routes for managing orders
Route::get('orders', [OrderController::class, 'index']);
Route::post('orders', [OrderController::class, 'store']);
Route::get('orders/{order}', [OrderController::class, 'show']);
Route::put('orders/{order}', [OrderController::class, 'update']);
Route::delete('orders/{order}', [OrderController::class, 'destroy']);
// Routes for managing payments
Route::get('payments', [PaymentController::class, 'index']);
Route::post('payments', [PaymentController::class, 'store']);
Route::get('payments/{payment}', [PaymentController::class, 'show']);
Route::put('payments/{payment}', [PaymentController::class, 'update']);
Route::delete('payments/{payment}', [PaymentController::class, 'destroy']);
// Routes for managing options
Route::get('options', [OptionController::class, 'index']);
Route::post('options', [OptionController::class, 'store']);
Route::get('options/{option}', [OptionController::class, 'show']);
Route::put('options/{option}', [OptionController::class, 'update']);
Route::delete('options/{option}', [OptionController::class, 'destroy']);
// Routes for managing option values
Route::get('option-values', [OptionValueController::class, 'index']);
Route::post('option-values', [OptionValueController::class, 'store']);
Route::get('option-values/{optionvalues}', [OptionValueController::class, 'show']);
Route::put('option-values/{optionvalues}', [OptionValueController::class, 'update']);
Route::delete('option-values/{optionvalues}', [OptionValueController::class, 'destroy']);
// Routes for managing item options
Route::get('item-options', [ItemOptionController::class, 'index']);
Route::post('item-options', [ItemOptionController::class, 'store']);
Route::get('item-options/{itemoptions}', [ItemOptionController::class, 'show']);
Route::put('item-options/{itemoptions}', [ItemOptionController::class, 'update']);
Route::delete('item-options/{itemoptions}', [ItemOptionController::class, 'destroy']);
// Routes for managing order items
Route::get('order-items', [OrderItemController::class, 'index']);
Route::post('order-items', [OrderItemController::class, 'store']);
Route::get('order-items/{orderItem}', [OrderItemController::class, 'show']);
Route::put('order-items/{orderItem}', [OrderItemController::class, 'update']);
Route::delete('order-items/{orderItem}', [OrderItemController::class, 'destroy']);
// Routes for managing QR codes
Route::get('qr-codes', [QRCodeController::class, 'index']);
Route::post('qr-codes', [QRCodeController::class, 'store']);
Route::get('qr-codes/{qrCode}', [QRCodeController::class, 'show']);
Route::put('qr-codes/{qrCode}', [QRCodeController::class, 'update']);
Route::delete('qr-codes/{qrCode}', [QRCodeController::class, 'destroy']);
// Routes for managing table layouts
Route::get('table-layouts', [TableLayoutController::class, 'index']);
Route::post('table-layouts', [TableLayoutController::class, 'store']);
Route::get('table-layouts/{tableLayout}', [TableLayoutController::class, 'show']);
Route::put('table-layouts/{tableLayout}', [TableLayoutController::class, 'update']);
Route::delete('table-layouts/{tableLayout}', [TableLayoutController::class, 'destroy']);
});
Im using Postman to test my laravel application backend. I am running my db on docker. None of the routes are working, however Im displaying categories as reference here. I tried to run the post method but it fails
2
Answers
Silly mistake:
Had to mention base path in the bootstrap->cache->app.php
Delete the route cache with the command:
php artisan route:clear
and cache the path again:
php artisan route
Finally, as kris-gjika said, print the route list to ensure that the routes are correct