如何从另一个类Laravel调用函数


How to call function from another class Laravel

我的Helpers文件夹中有payroll PayrollProcessController和PayrollHelper类。

这是我的PayrollProcessController代码:
<?php
namespace App'Http'Controllers;
use App'Http'Controllers'Base'MasterController;
use App'Http'Requests;
use App'Models'PayrollPeriod;
use App'Helpers'PayrollHelper;
use Illuminate'Http'Request;
use Illuminate'View'View;
use Yajra'Datatables'Datatables;
class PayrollProcessController extends MasterController
{
protected $indexView = 'payroll_process.index';
protected $detailView = 'payroll_process.detail';
protected $routeBindModel = 'payroll_process';
protected $redirectPageWhenFormSuccess = 'payroll_process.index'; //Route Name
protected $title = 'Payroll Process';
public function render(View $view, $route = null, $obj = null, $method = 'POST')
{
    $payrollPeriodName = PayrollPeriod::pluck('name','id');
    $view->with(compact('payrollPeriodName'));
    return parent::render($view, $route, $obj, $method);
}
}
这是我的PayrollHelper代码:
<?php
namespace App'Helpers;
use App'Helpers'Exceptions'ValidationException;
use App'Helpers'GeneralHelper;
use App'Models'Config;
use App'Models'Driver;
use App'Models'DriverPayable;
use App'Models'PayrollPeriod;
use Carbon'Carbon;
use Exception;
use Illuminate'Support'Facades'Log;
final class PayrollHelper {
public static function processPayroll(PayrollPeriod $payrollPeriod) 
{
    try {
        $drivers = Driver::where('active', true)->get();
        foreach ($drivers as $driver) {
            $payable = new DriverPayable;
            $payable->payrollPeriod()->associate($payrollPeriod);
            $payable->driver()->associate($driver);
            if (!$payable->save()) {
                throw new ValidationException($payable->errors());
            }
        }
    } catch (Exception $e) {
        Log::error($e);
        SessionHelper::setMessage('Unable to process payroll, Please contact system Administrator');
    } catch (ValidationException $e) {
        Log::info($e->errors);
        SessionHelper::setMessage($e->errors);
    }
}
}
?>

如何调用processPayroll函数

如有任何帮助,不胜感激

试试这个…在控制器中使用helper

use App'Helpers'PayrollHelper;
这是你的控制器构造函数
public function __construct(PayrollHelper $payroll_helper)
{
  parent::__construct();
  $this->payroll_helper = $payroll_helper;
}

像这样调用方法

$this->payroll_helper->processPayroll();

你的控制器看起来像这样

<?php
namespace App'Http'Controllers;
use App'Http'Controllers'Base'MasterController;
use App'Http'Requests;
use App'Models'PayrollPeriod;
use App'Helpers'PayrollHelper;
use Illuminate'Http'Request;
use Illuminate'View'View;
use Yajra'Datatables'Datatables;
class PayrollProcessController extends MasterController
{
  protected $indexView = 'payroll_process.index';
  protected $detailView = 'payroll_process.detail';
  protected $routeBindModel = 'payroll_process';
  protected $redirectPageWhenFormSuccess = 'payroll_process.index'; //Route Name
  protected $title = 'Payroll Process';
  public function __construct(PayrollHelper $payroll_helper)
  {
    parent::__construct();
    $this->payroll_helper = $payroll_helper;
  }
  public function render(View $view, $route = null, $obj = null, $method = 'POST')
  {
    $payrollPeriodName = PayrollPeriod::pluck('name','id');
    $view->with(compact('payrollPeriodName'));
    //Use where you want
    $this->payroll_helper->processPayroll();
    return parent::render($view, $route, $obj, $method);
  }
}

我只是想分享我所做的,现在还不知道它的副作用,但是我们会,

在控制器中使用helper

use App'Helpers'PayrollHelper;

在可以使用Payroll helper中的某个函数的函数中,执行

$payrollhelper = new PayrollHelper();

然后调用helper中的任意函数执行

$payrollhelper->funtion_name()...

下面是你如何以这种方式应用你的类的例子

  1. 导入控制器类
use App'Http'Controllers'CategoryController;
  • 创建该类的实例
  • $category_instance = new CategoryController();
    
    调用方法
    $category_instance->convert_json_serial_numbers_to_arrays($serial_number);