hasibweb's avatar

Auth not found issue

I am getting the following error.

Auth not found

Here is my code-

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Exceptions\InputException;

class OrderController extends Controller {

    public function index(Request $request) 
    {
        $user = Auth::user();
        $productId = $request->route('productId');

        $product = Product::where('id', $productId)->first();

        return $product;
    }

What wrong I did?

0 likes
2 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

@hasibweb

You forgot to import the Auth top of your class. It should be like this-

use Auth;

Here should be your updated code-

<?php

namespace App\Http\Controllers;

use Auth;
use Illuminate\Http\Request;
use App\Exceptions\InputException;

class OrderController extends Controller {

    public function index(Request $request) 
    {
        $user = Auth::user();
        $productId = $request->route('productId');

        $product = Product::where('id', $productId)->first();

        return $product;
    }

Now it should work fine.

8 likes

Please or to participate in this conversation.