Reputation: 797
I'm currently reading up on proper MVC, and I'm refactoring some code of my current project. It's written in Laravel. I'll have simplified the problem to this:
I have a domain object 'User' and 'Log'. A User has many Logs.
User domain object:
class User extends Authenticatable implements UserInterface
{
protected $fillable = ['surname', 'name','email'];
protected $hidden = [
'password', 'remember_token',
];
public function logs()
{
return $this->hasMany('App\Models\Log');
}
}
Log domain object:
class Log extends Model implements LogInterface
{
public $timestamps = false;
protected $fillable = [
'log_date',
'log_hours',
'log_minutes'
];
protected $dates = ['log_date'];
public function user()
{
return $this->belongsTo('App\Models\User');
}
}
In my view, I want to display the total hours logged of a certain day. I'm currently injecting a class in my controller which has a method calcHoursPerDay($user_id) which injects the LogRepository to get the Logs of that user on a given date and return the calculated value to the view.
Reading up on proper MVC, I'm inclined to drop the class and create a method for my User domain object called HoursOnDay($date). So keeping the bussiness logic in the Model.
Is this the way of using MVC, and if not, what would the proper approach?
Upvotes: 1
Views: 52
Reputation: 163748
Yes, that's the proper way to do things in Laravel and MVC. calcHoursPerDay()
should be part of User or Log model. Also, look at scopes doc, I guess that's what you want to use:
https://laravel.com/docs/5.1/eloquent#query-scopes
Upvotes: 1