Function call undefined: Laravel 5.1

I have a private function, as described below in the controller.

private function GetProjects($ProjectStatus) { return \App\Models\Project\Project_Model ::where('ProjectStatusID', $ProjectStatus) ->where('WhoCreatedTheProject', auth()->user()->UserID)->get(); } 

The following is an action method that uses this private function.

 public function ClientCancelledProjects() { $ProjectStatus = \App\Enumeration\Project\ProjectStatus::Cancelled; $MyProjects = GetProjects($ProjectStatus); return view("Project.Client.MyProject", array("Projects" => $MyProjects)); } 

The following is an error starting the controller.

Calling the undefined function App \ Http \ Controllers \ Project \ GetProjects ()

Does anyone know why this is happening? I am trying to reuse some lines of code as they are written repeatedly in the controller.

+5
source share
2 answers

To access controller functions from a function in the same controller, use self::

 public function ClientCancelledProjects() { $ProjectStatus = \App\Enumeration\Project\ProjectStatus::Cancelled; $MyProjects = self::GetProjects($ProjectStatus); return view("Project.Client.MyProject", array("Projects" => $MyProjects)); } 

Note: self:: (in upper case) will work depending on the installed php version, but for older versions self:: preferable.

Please see this link for more information: PHP - Self vs $ this

+3
source

Functions within a class are not global functions and cannot be called in this way. Instead, you need to use $this->GetProjects() .

+2
source

Source: https://habr.com/ru/post/1239087/


All Articles