如何从另一个类 Laravel 调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39952909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call function from another class Laravel
提问by Andrew Vanusi
I have payroll PayrollProcessController and PayrollHelper class in my Helpers folder.
我的 Helpers 文件夹中有 payroll PayrollProcessController 和 PayrollHelper 类。
here's my PayrollProcessController code :
这是我的 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);
}
}
and here's my PayrollHelper Code :
这是我的 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);
}
}
}
?>
how to call the processPayroll function ?
如何调用 processPayroll 函数?
Any help will be appreciated
任何帮助将不胜感激
回答by Komal
Try this.. Use helper in your controller
试试这个.. 在你的控制器中使用 helper
use App\Helpers\PayrollHelper;
This is your controller constructor
这是您的控制器构造函数
public function __construct(PayrollHelper $payroll_helper)
{
parent::__construct();
$this->payroll_helper = $payroll_helper;
}
Call method like this
调用方法是这样的
$this->payroll_helper->processPayroll();
UpdatedYour controller look like this
更新你的控制器看起来像这样
<?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);
}
}