Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

brunokaue's avatar

I can't access header in controller construct

Hello guys, before I start, I apologize for my English.

I have a controller called AuthController, this controller just used when app have a project like link/project/{project} In this controller i have a cosntruct, it checks the request, if ajax need to be in header an api_token, if not, just use User::where('id', Auth::id())->first()

When i put in digital ocean this not working.

Someone can help me?

0 likes
10 replies
brunokaue's avatar

@DEVFREY - When i see on browser the header api_token are present, but when i return a response with value ... i get NULL. In my machine i have the same code, and i GET the api_token value.

brunokaue's avatar

class AuthController extends Controller
{

    protected $user,  $apiToken;

    public function __construct(Request $request, $clinic)
    {
        $this->apiToken = $request->header('api_token');

        $this->middleware(function ($request, $next) {
            if($request->ajax()){
                if(!$this->apiToken){
                    return response()->json(['message' => 'Authentication required'],406);
                }
                return response()->json(['message' => $this->apiToken],406);
            }else{
                $this->user = User::where('id', Auth::id())->first();
                if(!$this->user){
                    return redirect()->route('login');
                }
            }
            return $next($request);
        });
    }
}

Look, in my macbook it`s work fine, in digital ocean server not. https://www.facebook.com/brun0.k4u3/videos/656860621419463/ < video

munazzil's avatar

In this case your getting first data only using first() because Auth::id would be something different than first data$this->user = User::where('id', Auth::id())->first(); try instead with below one because it getting every data which you have in database using get(),

   $this->user = User::where('id', Auth::id())->get();
devfrey's avatar

@MUNAZZIL - Could you please stop posting answers that are plainly incorrect?

@brunokaue - Try removing the `$apiToken property from your controller class. Also remove this:

$this->apiToken = $request->header('api_token');

Then inside your middleware closure ($this->middleware(...)), use $apiToken = $request->header('api_token').

1 like
devfrey's avatar

@MUNAZZIL - I'll mind anyone's business if they're spreading misinformation regularly.

2 likes
MThomas's avatar

@munazzil sorry your answer does not make sense, there is nothing wrong with $this->user = User::where('id', Auth::id())->first(); the only viable comment about that part would have been that $this->user = Auth::user(); would have had the same result and event more effective.

->first() will get you the first result if there will be more, as ID is a primary key there can only be one result, so using ->get() will let you endup with a collection with one item and you need to add a loop or pull the first.

So yeah, your answer does not make sense at all.

1 like

Please or to participate in this conversation.