Reputation: 568
I've a PermissionController
with a permission()
function like below, and i want to call the permission()
function in another controller. How can i call my permission()
function to another controller ?
here my PermissionController
class PermissionController extends Controller
{
public function permission(){
$checkPermission = User::leftJoin('employees', 'employees.user_id', '=', 'users.id')
->leftJoin('positions', 'employees.position_id', '=', 'positions.id')
->leftJoin('divisions', 'employees.division_id', '=', 'divisions.id')
->where(function($query){
$query->where('division_name', 'Sales')
->orWhere('division_name', '=', 'Project Management')
->orWhere('position_name', '=', 'Dept Head');
})
->get();
}
}
Upvotes: 2
Views: 8791
Reputation: 2736
Try to do like this
public function __construct(PermissionController $permission_controller)
{
parent::__construct();
$this->permission_controller = $permission_controller;
}
Call any method like this
$this->permission_controller->permission();
Upvotes: 1
Reputation: 1688
You can call your PermissionController's "permission" method this way :
app(PermissionController::class)->permission();
However, be aware that this method isn't a good idea at all.
You have multiple refactoring options available ranging from the use of a helper class, a method on the concerned model or using a laravel job.
Upvotes: 1
Reputation: 163748
You can call another controller's method, but this is terrible practice. Move you code to a model and call it with:
class PermissionController extends Controller
{
public function permission()
{
$checkPermission = User::getEmployeePermissions();
}
}
Do the same in other controllers' methods.
Upvotes: 2
Reputation: 67505
Create new Helper (e.g PermissionHelper.php
) then move the funtion to it and call it where you want using :
PermissionHelper::permission();
Hope this helps.
Upvotes: 2
Reputation: 2166
I think your function should be in one of your model since you're only getting value.
This will me more relevant according to MVC.
Having it in your User
model will let you use it on every instance of your application with a single call.
Upvotes: 1