Method 1: Using":app()" Helper Method
The 1st method is to use the "app()" helper method to get the other controller instance and then from the instance itself you can call the method. For this example let's say you have PostController and you want to get the method called "specialPostID()" method that returns some special ID. It might sound weird but do refer to the code below.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class PostController extends Controller { public function specialPostID() { return [1, 2, 3, 4]; } }
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class UserController extends Controller { public function show() { $specialIDS = app('App\Http\Controllers\PostController')->specialPostID(); dd($specialIDS); // [1, 2, 3, 4] } }
Using App::make() Facade
Just like the "app()" helper method like above, you can also make use of the "App" facade and call in the "make()" method.
<?php $specialIDS = App::make('App\Http\Controllers\PostController')->specialPostID(); dd($specialIDS); // [1, 2, 3, 4]
Other Reads
- How to get Service Container in Laravel
Leave a reply