I have 2 Eloquent models: Project and Task. They both have deadlines and I have 3 deadline-related functions that both classes must have, but I don't want to have duplicated code.
The only solution, I came up with, is to extend Eloquent to some class DeadlineEloquent and extend Project and Task with this new class. But this doesn't sound like the best solution, having the fact that Laravel doesn't have place to store such kind of classes (it is not a model, I don't know what it is) as far az I know. How would you proceed in this case?
Laravel doesn't really have a place to store anything, you are free to do whatever you need with your application folders and files. But if you still have your app/models folder around, you can create a BaseModel and store in it. This is the way most people would go with:
class BaseModel extends Eloquent {
public function whatever()
{
}
}
class Project extends BaseModel {
}
class Project extends BaseModel {
}
If you are on PHP 5.5, you can use traits:
trait Functions {
public function whatever()
{
}
}
class Project extends BaseModel {
use Functions;
}
class Project extends BaseModel {
use Functions;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With