jeudi 24 mai 2018

Laravel: What is the best way to implement a method in a model class

I have a simple search method in my Game model and I have implemented it as below.

public static function search($season, $week)
{
     $filteredGames = Game::with('season', 'week', 'homeTeam', 'awayTeam')
        ->when($season != null, function ($q) {
            return $q->where('season_id', request('season'));
        })->when($week != null, function ($q) {
            return $q->where('week_id', request('week'));
        })
        ->paginate(15);

       return $filteredGames;
}

And using it in controller like this

$games = Game::search(request('season'), request('week'));

Looks like it works perfectly.

I want learn whether using a static method is the best way to implement such a feature in terms of design patterns and SOLID principles or not.

Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire