Navigate back to the homepage

Laravel call function from another class

Infinitbility
Laravel
October 11th, 2020 · 1 min read
Laravel call function from another class

Hello Friends,

Welcome To Infinitbility!

If you are stuck calling function from another controller, relax you will definitely get a solution from this article.

If you want to use controller function please follow the business logic structure of laravel. sometimes it’s the reason of memory leak and security issue. I will never recommend using this method on your project.

here I will share two controllers to easily understand the calling a function from another controller.

Child Controller

1<?php
2
3namespace App\Http\Controllers;
4
5use App\Http\Controllers\Controller;
6use Illuminate\Http\Request;
7
8class ChildController extends Controller
9{
10
11 /**
12 * Common function using on multiple controller
13 */
14 public function Calculation($firstNumber, $operator, $secondnumber){
15
16 if($operator == '+'){
17 $response = $firstNumber + $secondnumber;
18 }elseif($operator == '-'){
19 $response = $firstNumber - $secondnumber;
20 }elseif($operator == '*'){
21 $response = $firstNumber * $secondnumber;
22 }elseif($operator == '/'){
23 $response = $firstNumber / $secondnumber;
24 }
25
26 return $response;
27 }
28
29}

Parent Controller

1<?php
2
3namespace App\Http\Controllers;
4
5use App\Http\Controllers\Controller;
6use Illuminate\Http\Request;
7use App\Http\Controllers\ChildController;
8
9class ParentController extends Controller
10{
11 protected $ChildController;
12 public function __construct(ChildController $ChildController)
13 {
14 $this->ChildController = $ChildController;
15 }
16
17 /**
18 * Parent function using child function
19 */
20 function calc(Request $Request){
21
22 $firstNumber = $Request->input('firstNumber');
23 $operator = $Request->input('operator');
24 $secondnumber = $Request->input('secondnumber');
25
26 $response = $this->ChildController->Calculation($firstNumber, $operator, $secondnumber);
27 return $response;
28 }
29}

if you get help, please share post on your social network

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

More articles from Infinitbility

React native ScrollView scroll to position

React native ScrollView scroll to position

The simplest way of scroll to position in react native using ScrollView ref.

October 11th, 2020 · 1 min read
laravel pagination with customization

laravel pagination with customization

If you want to customize your pagination view and want to understand the flow of Laravel pagination then you are right place.

October 11th, 2020 · 1 min read
© 2020–2021 Infinitbility
Disclaimer
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]