Home > other >  How to call Service class function?
How to call Service class function?

Time:08-19

use App\Http\Controllers\ATransaction;
use App\Service\ATransactionServices;

class TransactionController extends Controller {  

   public function ATransaction(Request $request, ATransactionServices $ATransaction){

        try {
            $validated_request = $this->validateRequest($request);
         } catch (ValidationException $exception) {
            return redirect()->back()->withInput()->withErrors($exception->errors());
        }

How to call my service class function to replace into

$validated_request = $this->validateRequest($request);

Can someone show me the right way?

CodePudding user response:

You can traits concepts or can directly use DI ( dependency Injection ) to implement service into this class.

For traits, you can check How to create traits in Laravel

And if you want to use Dependency Injection then do something like:

public function __construct(ATransactionServices $aTransactionServices) 
{
    $this->aTransactionServices = $aTransactionServices;
}

Then inside the method you can directly use the service methods:

$this->aTransactionServices->methodName();

Hope this works for you and resolves your issue. have a great day.

CodePudding user response:

$validated_request = $ATransaction->validateRequest($request);
  • Related