0

I created class which load DB data.

   <?php

namespace App\Repositories;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class FantasticRepository
{
        public function getFantastic($id)
        {
            $user = Auth::user();
            $companyId = $user->id;
            $tableName = "elites";
            return \DB::table($tableName )->where('id', $id)->first();
       }

}

But a controller can not call a function of the class I created.

use App\Repositories\FantasticRepository;

class FooController extends Controller
{
    private FantasticRepository $fantasticRepository;
    public function getFantastic(Request $request)
    {

       $ke = $fantasticRepository->getInformation($request->id);
      return view('ke.index',compact($ke));
    }
     public function getInformation($id){
        $ko = \DB::connection('second')->table("pens" )->where('user_id', $id)->first();

      return view('ko.index',compact($ko));
}

   }

My Error log says

local.ERROR: Undefined variable: fantasticRepository {"userId":4,"exception":"[object] (ErrorException(code: 0): Undefined variable: fantasticRepository at /Users/Developments/abc/app/Http/Controllers/FooController.php:36) [stacktrace] #0 /Users/Developments/app/Http/Controllers/FooController.php(36): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Undefined varia...', '/Users/...', 36, Array)

Is a laravel app able to use an external class?

enter image description here

1
  • use correct method use App\Repositories\FantasticRepository; class FooController extends Controller { use FantasticRepository; public function getFantastic(Request $request) { $ke = $this->getFantastic($request->id); return view('ke.index',compact($ke)); } } Commented Feb 21, 2022 at 17:35

3 Answers 3

1

in Laravel we can import call with use.

use App\Repositories\FantasticRepository;
class FooController extends Controller 
{     
   use FantasticRepository;     
   
   public function getFantastic(Request $request)     
   {         
        $ke = $this->getFantastic($request->id);
        return view('ke.index',compact($ke));     
   }    
}
Sign up to request clarification or add additional context in comments.

1 Comment

I don't want to use trait , having the same name function in FooController.
0

You need dependency injection. You can do that on constructor or in function argumants.

public function __construct(private FantasticRepository $fantasticRepository) {}

//or

public function getFantastic(Request $request, FantasticRepository $fantasticRepository) {}



2 Comments

I tried both. However, nothing changed.
Did you read error message you added recantly. It says "unexpected private." So it is a synax error, which means it's not about class and you don't use PHP 8. You need to define your constructor older way or update your PHP version. If another error pops up we can look at it.
0

You cannot use the private function outside of its class so you have to define getFantastic function as public function to use it in controller

 private function getFantastic($id)
        {
            $user = Auth::user();
            $companyId = $user->id;
            $tableName = "elites";
            return \DB::table($tableName )->where('id', $id)->first();
       }

to

 public function getFantastic($id)
        {
            $user = Auth::user();
            $companyId = $user->id;
            $tableName = "elites";
            return \DB::table($tableName )->where('id', $id)->first();
       }

1 Comment

I changed it from private to public. However, the same error still happen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.