[英]How to call function from another class Laravel
我的 Helpers 文件夹中有工资单 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 函数?
任何帮助将不胜感激
试试这个..在你的控制器中使用助手
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);
}
}
我只是想分享我所做的,还不知道这样做的副作用,但我们会,
在控制器中使用助手
use App\Helpers\PayrollHelper;
而在 Payroll helper 里面的函数之一的函数里面,做
$payrollhelper = new PayrollHelper();
然后调用助手中的任何函数
$payrollhelper->funtion_name()...
这对我有用
这是如何以这种方式应用课程的示例
use App\Http\Controllers\CategoryController;
$category_instance = new CategoryController();
$category_instance->convert_json_serial_numbers_to_arrays($serial_number);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.