How to add new method chaining for laravel eloquent?

Member

by samara , in category: PHP Frameworks , a year ago

How to add new method chaining for laravel eloquent?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dana , a year 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.

Related Threads:

How to join 3 tables using laravel eloquent?
How to get a all eloquent models in laravel?
How to use substring with laravel eloquent?
How to prevent duplicates in laravel eloquent?
How to chain eloquent relation in route in laravel?
How to convert sql query to eloquent in laravel?