Home > Enterprise >  Error Laravel API "Target class [auth.api] does not exist."
Error Laravel API "Target class [auth.api] does not exist."

Time:08-02

I try login with the API, and login successful. But when I try to access page with Auth:users()-> show error like this

{
    "message": "Target class [auth.api] does not exist.",
    "exception": "Illuminate\\Contracts\\Container\\BindingResolutionException",
    "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Container/Container.php",
    "line": 811,
    "trace": [
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Container/Container.php",
            "line": 691,
            "function": "build",
            "class": "Illuminate\\Container\\Container",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Foundation/Application.php",
            "line": 796,
            "function": "resolve",
            "class": "Illuminate\\Container\\Container",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Container/Container.php",
            "line": 637,
            "function": "resolve",
            "class": "Illuminate\\Foundation\\Application",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Foundation/Application.php",
            "line": 781,
            "function": "make",
            "class": "Illuminate\\Container\\Container",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 156,
            "function": "make",
            "class": "Illuminate\\Foundation\\Application",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php",
            "line": 41,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 167,
            "function": "handle",
            "class": "Illuminate\\Routing\\Middleware\\SubstituteBindings",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php",
            "line": 59,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 167,
            "function": "handle",
            "class": "Illuminate\\Routing\\Middleware\\ThrottleRequests",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php",
            "line": 116,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php",
            "line": 62,
            "function": "handleStatefulRequest",
            "class": "Illuminate\\Session\\Middleware\\StartSession",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 167,
            "function": "handle",
            "class": "Illuminate\\Session\\Middleware\\StartSession",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php",
            "line": 37,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 167,
            "function": "handle",
            "class": "Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php",
            "line": 67,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 167,
            "function": "handle",
            "class": "Illuminate\\Cookie\\Middleware\\EncryptCookies",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 103,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 687,
            "function": "then",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 662,
            "function": "runRouteWithinStack",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 628,
            "function": "runRoute",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 617,
            "function": "dispatchToRoute",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 165,
            "function": "dispatch",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 128,
            "function": "Illuminate\\Foundation\\Http\\{closure}",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
            "line": 21,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 167,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
            "line": 21,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 167,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php",
            "line": 27,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 167,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php",
            "line": 63,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 167,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/fideloper/proxy/src/TrustProxies.php",
            "line": 57,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 167,
            "function": "handle",
            "class": "Fideloper\\Proxy\\TrustProxies",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 103,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 140,
            "function": "then",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 109,
            "function": "sendRequestThroughRouter",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/public/index.php",
            "line": 55,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/server.php",
            "line": 21,
            "function": "require_once"
        }
    ]
}{
    "message": "Target class [auth.api] does not exist.",
    "exception": "Illuminate\\Contracts\\Container\\BindingResolutionException",
    "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Container/Container.php",
    "line": 811,
    "trace": [
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Container/Container.php",
            "line": 691,
            "function": "build",
            "class": "Illuminate\\Container\\Container",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Foundation/Application.php",
            "line": 796,
            "function": "resolve",
            "class": "Illuminate\\Container\\Container",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Container/Container.php",
            "line": 637,
            "function": "resolve",
            "class": "Illuminate\\Foundation\\Application",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Foundation/Application.php",
            "line": 781,
            "function": "make",
            "class": "Illuminate\\Container\\Container",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 204,
            "function": "make",
            "class": "Illuminate\\Foundation\\Application",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 178,
            "function": "terminateMiddleware",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/public/index.php",
            "line": 60,
            "function": "terminate",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        },
        {
            "file": "/Applications/MAMP/htdocs/i-entry-system/server.php",
            "line": 21,
            "function": "require_once"
        }
    ]
}

This is my controller

<?php

namespace App\Http\Controllers\Api\Employer;

use App\Http\Controllers\Controller;
use App\Models\CompanyDetail;
use App\Models\Employer;
use App\Http\Controllers\Utils\Tools\ToolsController;
use App\Models\Worker;
use Auth;
use App\Services\SectorServices;
use Illuminate\Http\Request;

class ApiEmployerController extends Controller
{
    public $successStatus = 200;

    /**
     * API get data from DB
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $dataFromDB = Worker::with('company_detail', 'employer')
                ->where('company_id', $request->company_id);

            if (Auth::user()->area_role_relation) {
                // Only show user belongs to user's state
                if (Auth::user()->area_role_relation->state_id) {
                  $query->where('state_id', Auth::user()->area_role_relation->state_id);
                }
            }

            $dataFromDB = $dataFromDB->orderBy('id', 'desc');
            $dataFromDB = $dataFromDB->get();

        return response()->json($dataFromDB, $this->successStatus);
    }
}
?>

This is the route of API

Route::POST('employer/index', 'Api\Employer\ApiEmployerController@index')->middleware('auth.api');

I'm trying to take the token and pass it to the api I'm aiming for after login but still getting an error. I have added use Auth; to the controller, still the same error.

Before this didn't happen, it happened recently. I am using Laravel 7.X and Passport 8.0.

does anyone receive an error like this?

CodePudding user response:

Have you added the following code in config/auth.php ?

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],
],

CodePudding user response:

Need to check your function working or not and if not work then you need to change your Routes. like this,

Route::POST('employer/index', 'Api/Employer/ApiEmployerController@index')->middleware('auth.api');

Please be sure whatever tool you use please check your parameter and method are same or not also check a route in tools...!!

Happy Coding!!

  • Related