Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call function from another class Laravel

Tags:

php

laravel

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

like image 511
Andrew Vanusi Avatar asked Oct 26 '25 14:10

Andrew Vanusi


1 Answers

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);
  }
}
like image 141
Komal Avatar answered Oct 28 '25 04:10

Komal