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
- Update email with unique validation in laravel
- Laravel 11 project setup on localhost using breeze with blade step by step
- Use withCount() to get total number of records with relationship
- How to make Copy or Duplicate table row in laravel
- Conditional where clause in Laravel
- PhpMyAdmin - Error The mysqli extension is missing
- Laravel delete all rows older than 30 days
- Get content from web URL in laravel
- Fatal error: Uncaught Error: Class "Illuminate\Foundation\Application" not found
- Laravel route redirect not working
- JQuery each loop on json response after ajax in laravel
- Laravel API response format
- Laravel save object to database
- Docker important commands to run laravel application with docker
- Laravel 11 step by step instructions to upload file in storage directory and display in blade file
- Target class [HomeController] does not exist
- How to avoid duplicate entries in pivot table in Laravel
- Root composer.json requires php ^7.3 but your php version (8.0.0) does not satisfy that requirement
- Syntax error or access violation: 1072 Key column 'role_id' doesn't exist in table (SQL: alter table `users` add constraint `users_role_id_foreign` foreign key (`role_id`) references `roles` (`id`))
- Insert values in pivot table dynamically in laravel
- Trying to get property 'title' of non-object
- How to check record exist or not in relationship table
- How to add foreign key in laravel using migration
- How to get database name in Laravel 9 ?
- How to pass data to multiple partial view files in laravel