I have payroll PayrollProcessController and PayrollHelper class in my Helpers folder.
here's my PayrollProcessController code :
<?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 :
<?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 ?
Any help will be appreciated
Try this.. Use helper in your controller
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();
Updated Your 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);
}
}
I just want to share what I have done, Don't know yet the side effects of this but we'll,
Use helper in your controller
use App\Helpers\PayrollHelper;
And inside the function that is good to consume one of the functions inside Payroll helper, do
$payrollhelper = new PayrollHelper();
Then call any function within the helper doing
$payrollhelper->funtion_name()...
This worked for me
Here is the example how you can apply your class in that way
use App\Http\Controllers\CategoryController;
$category_instance = new CategoryController();
$category_instance->convert_json_serial_numbers_to_arrays($serial_number);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.