Reputation: 581
I've working in a project and I noticed have many same methods for differents models:
Class1::where('id', $id)
->update($request->except('_token'));
Class2::where('id', $id)
->update($request->except('_token'));
Class3::where('id', $id)
->update($request->except('_token'));
So I think is so repetitive have those methods in each class. Is there any way to make a generic method and use it in my controller, like this?
AnyClass::genericMethod($id);
Thank you!
Upvotes: 1
Views: 849
Reputation: 163748
You can use trait for methods like this:
trait SomeTrait
{
public function genericMethod($id)
{
return $this->where('id', $id)->update(request()->except('_token'));
}
}
And add it to as many models as you like:
class Class3 extends Model
{
use SomeTrait;
Upvotes: 2