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?

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)); } }