1

I am having problems with a controller returning just an object. I have a tables types and in TypesController I want a function that will just return an array of types so I can use it in another controller so I wrote in TypesController:

public function getNavTypes()
    {
        $types = DB::table('v_itemtypes')->get();
        return ($types);
    }

I want to call this in another controller (authors) so I added the line

use App\Http\Controllers\TypeController;

And want to just call the object in my method to pass on to the view:

 public function getAuthor($author_id)
    {

        $author = Author::where('id', $author_id)->first();
        $navtypes = TypeController::getNavTypes;
        return view('authors.edit',['author'=>$author, 'navtypes'=>$navtypes]);
    }

but I am getting the following error:

FatalErrorException in AuthorController.php line 28: Undefined class constant 'getNavTypes'

Line 28 is

$navtypes = TypeController::getNavTypes;

As a newbie I am doing something wrong, but what?

Jim
  • 596
  • 1
  • 10
  • 36

4 Answers4

1

You cannot call a function without instantiating its class except if the function is static. So either you change your function to static:

public static function getNavTypes()

Or you instantiate an object of the controller first:

$typeController = new TypeController();
$navtypes = $typeController->getNavTypes();

And ah btw since getNavTypes is a function, you need to add two brackets:

()

before its name when calling it. So change $navtypes = TypeController::getNavTypes; to $navtypes = TypeController::getNavTypes();

Hope this helps.

Osama Sayed
  • 1,993
  • 15
  • 15
0

I think you should use TypeController::getNavTypes(), as getNavTypes is a method, not a class constant.

Hugo
  • 109
  • 1
  • 1
  • 8
0

Calling methods with :: is only possible if you specified your method as static.

public static function myMethod()
{
    // Do stuff...
}

In your case, the method is not static so you need to call your method like this:

$TypeController = new TypeController();
$navtypes = $TypeController->getNavTypes();

In Laravel you can also create a controller instance like this:

$TypeController = App::make('TypeController');
$navtypes = $TypeController->getNavTypes();

Note: Your getNavTypes() method logically belongs inside a Model or Repository class. So you better move it out of the controller and place it inside a model class of your choice - probably where it fits best regarding the context. In your controller mostly action methods should be placed.

codedge
  • 4,754
  • 2
  • 22
  • 38
0

You can call controller methods in different controllers in Laravel 5 like:

app('App\Http\Controllers\TypeController')->getNavTypes()

Reference: Access Controller method from another controller in Laravel 5

According to your code: getNavTypes is a method, not a static property or constant. You can call it this way

TypeController::getNavTypes();

Also declare this method static like before calling it:

public static function getNavTypes()
Community
  • 1
  • 1
kazimt9
  • 563
  • 5
  • 11