How To Use Method Chaining And Using It To Simplify Your Laravel Code

Code Axion The Security Breach
4 min readMar 5, 2023

What is Method Chaining ?

Method chaining is a powerful technique in object-oriented programming that allows you to call multiple methods on the same object in a single line of code. It’s a great way to streamline your code and make it more readable and maintainable so instead of this :

we can do this:

See its looks much more Cleaner and Readable .

So how can we achieve this ?

To implement method chaining, each method must return the object instance using the $this keyword at the end of the method like this :

class MyClass {

private $property1;
private $property2;

public function sum1($value) {
$this->property1 = $value;
return $this; <---- returns the object instance
}

public function sum2($value) {
$this->property2 = $value;
return $this; <---- returns the object instance
}

public function getResult() {
$result = $this->property1 + $this->property2;
return $result;
}
}


$result = (new MyClass)->sum1(10)->sum2(20)->getResult();

dd($result);

//result: 30

The last line of code creates a new instance of the MyClass class using the parentheses and then immediately calls the sum1() method with the argument 10. This returns the object instance, which is then used to call the sum2() method with the argument 20. This also returns the object instance, which is then used to call the getResult() method. The value of $property1 is 10 and the value of $property2 is 20, so the getResult() method returns 30.

Well How can we use this in real life case ?

Suppose you have 4 tasks to do :

1. Create User

2. Attach Roles

3. Create User Company

4. Finally, get the user

Now let’s do this with method chaining .

Let’s create a user Service class called ‘ UserService’ in App\Services folder .

Here we will define some methods which will be :
createUser(), handleRole(), createCompany(), getUser().

As you can see on the first method we are creating the user by passing the request object in the parameter and after that we assign the newly created user to the user property by using :

$this->user = $user <--- assign the newly created user to user property  

and then we return the object itself which helps us to allow method chaining further.

Now as the user property has now our user object , we can attach the roles of that user in handleRole method and return the instance .

$this->user->roles()->attach([2,5]) <--- we can even attach eloquent methods 

You can see more about chaining methods examples on this article:

Article: Tired of Writing Image Upload Code ? Create this Reusable Service In Laravel .

Now in createCompany method , as our user object is still present in user property we can create the company by passing the request object in createCompany method and pass the user_id foreign key from the user property if you want you can even pass the request object in the constructor method . After creating the company we will return $this which will allow for method chaining again .

After creating the company we have the getUser() method which will get our currently created user .

To try this service we can chain these methods in our controller like this :

Now in $user variable we will have the newly created user.

Hopefully, you have gained a better understanding of how method chaining is applied in real-life situations.

If you liked this article, let me know what topic I should cover in the next one …

Happy Coding : )

--

--

Code Axion The Security Breach

A Developer Who Enjoys Code Refactoring, Optimizing, and Improving The performance of Applications