Home > Software design >  Additional Parameter categories for Laravel API Resources
Additional Parameter categories for Laravel API Resources

Time:09-02

I'm learning Laravel and it's API resource. I'm trying to understand how to pass additional parameters as I have an extra category with my resource.

With web.php route controllers i'm able to pass my extra parameter to the method (although this is not RESTFULL) to deal with the query.

Route::get('store/{root}/{parent}', [CategoryController::class, 'subCategory']);

However with the api.php resource only looks for the index and show methods.

So i'm trying to achieve something like: products/shoes/nike/{idOfNikeShoe}.

Do I need to create a new controller 'brands' and a new resource for this?

Api.php

Route::group(['prefix' => 'v1', 'namespace' => 'App\Http\Controllers\Api\V1'], function(){
    Route::apiResource('products', ProductController::class);
    Route::apiResource('products.brand-type', ProductBrandController::class);
});

I'm confused as the routes from web.php can go to different methods but the api.php only targets index,show,delete etc

CodePudding user response:

See if you have different requirement that is not exist in your resource route then you have to make one separate route for it method should be same but make another route like this:

Route::resource('categories', CategoryController::class);
Route::get('/categories/destroy/{id}', [CategoryController::class,'destroy'])->name('categories.destroy');

here I want to destroy object using Id so I have to make another route but method should be same you have to only change parameter of function.

  • Related