Ad

Laravel New Controller Method Doesn't Work

- 1 answer

When I create a new function in the controller for some reason it does not work. When I set the code from this function(getUnitsNotIn) to another function(index), that code works. Does anyone know why this is happening?

My UnitsController.php action

public function index(){
    $items = Unit::select('parent_id')->where('parent_id','!=',NULL)->get()->toArray();
    return Units::whereNotIn('id',$items)->get();
}

public function getUnitsNotIn(){
    $items = Unit::select('parent_id')->where('parent_id','!=',NULL)->get()->toArray();
    return Units::whereNotIn('id',$items)->get();
}

My api.php

Route::apiResource('/units', 'UnitController');
Route::get('/units/notIn', '[email protected]');

In short, any new controller function that I make will not work. I tried to make a new controller and the same thing happens. How to fix this problem?

Ad

Answer

It is not working because of apiResource(). Resource route assumes /units/{id}. so when you call /units/notIn route assume notIn as id And Action call show()

You need to use a different name.

Route::get('/un/notIn', '[email protected]');

Verb, path , action , route name

GET/units/{id}showunits.show

Ad
source: stackoverflow.com
Ad