I have a Laravel model with a simple function in it. But for some reason I get this error:
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
Here is my Model:
class Dish extends Model
{
public function sum() {
return $this->attributes['begin'] + 10;
}
}
In my controller I do:
$model->sum();
Anyone knows how I can add the function to my model?
Many thanks in advance!
If the calculation will be performed with the model data, you do not need to use $this->attributes
to get the model data, that way it actually makes it a bit more "dirty". the cleanest way it will be as mention in the comments:
public function sumBegin($default = 10)
{
return $this->begin + $default;
}
that way we take the begin
for the current model being called.