
Method chaining in Laravel
Created at 19-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
- 419 page expired error in Laravel
- How to disable timestamps in laravel
- Remove public from url in laravel project
- Wheredate in laravel not working
- SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'posts_user_id_foreign'; check that column/key exists
- Redirect to previous page or url in laravel
- How to check data inserted or deleted in pivot after toggle method
- Call to undefined relationship [user] on model [App\Models\Post]
- Update existing pivot table data in laravel
- Attempt to read property "avatar" on null in Laravel
- How to start websocket server in laravel
- Rename Pivot Table in Laravel
- How to get the random value form a specific column in laravel ?
- Composer\Exception\NoSslException
- Define variable and use in Laravel controller method
- How to insert value to additional columns in pivot table in laravel
- How to restore multiple records after soft-deletes in Laravel
- Delete file from amazon s3 bucket using Laravel
- How to delete record in Laravel with ajax
- If no route matched route::fallback in laravel
- How to get laravel errors folder in views directory in laravel
- Extra Filter Query on Relationships in Laravel
- Php artisan make model, factory, migration and controller in single command
- Calculate age from date of birth in Laravel
- Get content from web URL in laravel