Method chaining in Laravel
Created at 20-Jul-2023 ,
By samar
Method chaining in Laravel follows the same concept as method chaining in PHP.
To create method chaining in your own PHP class, you need to follow these steps:
- Define your class: Start by creating a class and defining the methods you want to chain. Each method should return the instance of the class ($this) to allow for chaining. Create a test directory in app folder of Laravel project and create a file Calculation.php in it add below code.
app\Test\Calculation.php
<?php
namespace App\Test;
class Calculation {
public $result;
public function __construct(){
$this->result = 0;
}
public function add($num){
$this->result += $num;
return $this;
}
public function substract($num){
$this->result -= $num;
return $this;
}
public function multiply($num){
$this->result *= $num;
return $this;
}
public function getResult(){
return $this->result;
}
}
- Chain the methods: Within each method, you can call other methods on $this to chain them together. This allows you to invoke multiple methods in a single statement.
routes\web.php
use App\Test\Calculation;
Route::get('/method-chain', function(){
return (new Calculation())->substract(10)->add(20)->multiply(5)->getResult();
});
- Now hit the url
http://localhost:8000/method-chain
in your browser. You will see the result.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- How to validate URL with https using regex in laravel
- Method Illuminate\Events\Dispatcher::fire does not exist
- Always load the relationship data with eager loading in Laravel
- PhpMyAdmin - Error The mysqli extension is missing
- How to upload multiple images after preview in laravel using cropper js
- How to create static page in Laravel
- How to implement toggleLike() method in Overtrue\LaravelLike laravel package
- How to check records exist in loaded relationship in Laravel blade view
- How to pass query string with pagination in laravel
- Trying to get property 'title' of non-object
- How to get records in random order in laravel
- How to get specific columns using Laravel eloquent methods
- SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
- How to add unique records in pivot columns of Laravel pivot table
- Laravel delete all rows older than 30 days
- Get previous date data in laravel
- Pass variable from blade to controller Laravel
- How to get tomorrow and yesterday date in laravel
- How to get the random value form a specific column in laravel ?
- Laravel create default admin user
- Convert multidimensional array to single array in Laravel
- Laravel API response format
- Get count of filter data, while return a small set of records
- How to create belongstomany relation using custom name on custom pivot table
- How to get database name in Laravel 9 ?