bigweld86
1 month ago
176
5
General

Separate controller methods seem to be messing with one another

Posted 1 month ago by bigweld86

Hi. I've got the following routes:

Route::group(['middleware' => ['guest:api', 'cors']], function () {

    /**
     * Grouped Routes for Attributes
     */
    Route::group(['prefix' => 'attributes'], function() {
        Route::get('/{id}', 'Admin\[email protected]')->name('admin.attributes.edit');
        Route::get('/frontend-types', 'Admin\[email protected]');
    });
});

With their respective controller methods:

public function edit($id) : JsonResponse
    {
        $attribute = $this->attribute->findAttributeById((int)$id);
        return $this->sendResponse([$attribute]);
    }

public function getFrontendTypes() : JsonResponse
    {
        return $this->sendResponse(Attribute::ATTRIBUTE_FRONTEND_TYPES);
    }

When I have these two routes active I'm getting an error when accessing getFrontendTypes. But what's weird is that the error comes from the edit method although they are not dependent on one another. If I commented out the edit route then getFrontendTypes works fine and viceversa. But when both are active edit is failing with the following error:

(this is the url I'm testing via Postman: http://127.0.0.1:8000/api/attributes/frontend-types):

 "message": "Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [App\Models\Attribute] 0 in /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:371\nStack trace:\n#0 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php(23): Illuminate\Database\Eloquent\Builder->findOrFail(0)\n#1 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1618): Illuminate\Database\Eloquent\Model->forwardCallTo(Object(Illuminate\Database\Eloquent\Builder), 'findOrFail', Array)\n#2 /Users/bigweld/Sites/csrecords/app/Services/BaseService.php(132): Illuminate\Database\Eloquent\Model->__call('findOrFail', Array)\n#3 /Users/bigweld/Sites/csrecords/app/Services/AttributeService.php(51): App\Services\BaseService->findOneOrFail(0)\n#4 /Users/bigweld/Sites/csrecords/app/Http/Controllers/Admin/AttributeController.php(57): App\Services\AttributeService->findAttributeById(0)\n#5 [internal function]: App\Http\Controllers\Admin\AttributeController->edit('frontend-types')\n#6 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array(Array, Array)\n#7 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\Routing\Controller->callAction('edit', Array)\n#8 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Routing/Route.php(219): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\Admin\AttributeController), 'edit')\n#9 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Routing/Route.php(176): Illuminate\Routing\Route->runController()\n#10 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Routing/Router.php(680): Illuminate\Routing\Route->run()\n#11 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(130): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))\n#12 /Users/bigweld/Sites/csrecords/app/Http/Middleware/RedirectIfAuthenticated.php(24): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))\n#13 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): App\Http\Middleware\RedirectIfAuthenticated->handle(Object(Illuminate\Http\Request), Object(Closure), 'api')\n#14 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))\n#15 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))\n#16 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(59): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))\n#17 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Routing\Middleware\ThrottleRequests->handle(Object(Illuminate\Http\Request), Object(Closure), 60, '1')\n#18 /Users/bigweld/Sites/csrecords/app/Http/Middleware/Cors.php(42): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))\n#19 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): App\Http\Middleware\Cors->handle(Object(Illuminate\Http\Request), Object(Closure))\n#20 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(105): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))\n#21 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Routing/Router.php(682): Illuminate\Pipeline\Pipeline->then(Object(Closure))\n#22 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Routing/Router.php(657): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))\n#23 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Routing/Router.php(623): Illuminate\Routing\Router->runRoute(Object(Illuminate\Http\Request), Object(Illuminate\Routing\Route))\n#24 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Routing/Router.php(612): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))\n#25 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))\n#26 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(130): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))\n#27 /Users/bigweld/Sites/csrecords/app/Http/Middleware/Cors.php(42): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))\n#28 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): App\Http\Middleware\Cors->handle(Object(Illuminate\Http\Request), Object(Closure))\n#29 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))\n#30 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))\n#31 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))\n#32 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))\n#33 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))\n#34 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))\n#35 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(62): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))\n#36 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))\n#37 /Users/bigweld/Sites/csrecords/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))\n#38 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Fideloper\Proxy\TrustProxies->handle(Object(Illuminate\Http\Request), Object(Closure))\n#39 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(105): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))\n#40 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\Pipeline\Pipeline->then(Object(Closure))\n#41 /Users/bigweld/Sites/csrecords/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))\n#42 /Users/bigweld/Sites/csrecords/public/index.php(55): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))\n#43 /Users/bigweld/Sites/csrecords/server.php(21): require_once('/Users/bigweld/...')\n#44 {main}",

but when I comment out this route: //Route::get('/{id}', 'Admin\[email protected]')->name('admin.attributes.edit');

then the previous test returns the right data:

{
  "success": true,
  "data": [
    {
      "id": "select",
      "value": "Select"
    },
    {
      "id": "radio",
      "value": "Radio"
    },
    {
      "id": "text",
      "value": "Text"
    },
    {
      "id": "text_area",
      "value": "Text Area"
    }
  ],
  "message": "Success"
}

what am I missing? what's going on here?

Thanks

Please sign in or create an account to participate in this conversation.