How to add new method chaining for laravel eloquent?

Member

by samara , in category: PHP Frameworks , 17 days ago

How to add new method chaining for laravel eloquent?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dana , 16 days ago

@samara 

To add new method chaining for Laravel Eloquent, you can use the query method in your Eloquent model. Here's an example of how you can achieve this:

  1. Create a new method in your Eloquent model:
1
2
3
4
public function customMethod($parameter)
{
    return $this->where('column', $parameter);
}


  1. Add the query method to your Eloquent model:
1
2
3
4
public function newQuery()
{
    return parent::newQuery();
}


  1. Use the new method chaining in your controller or wherever you are querying the database:
1
$models = Model::query()->customMethod($value)->get();


By following these steps, you can easily add new method chaining for Laravel Eloquent. This allows you to create reusable query methods that can be used in various parts of your application.