2 months ago

Laravel - Services or Repositories patterns

Posted 2 months ago by fdusautoir

Hi everyone,

Since a couple of days I find myself thinking and trying a lot of things about Services or Repositories patterns with Laravel. Until now, I always put data logic into Models like accesors, mutators, scopes and custom methods like this :

    public function createTrip(Request $request)
    	$quote = $this->create($this->syncDatas($request));
    	return $quote;

    public function updateTrip(Request $request)
    	return $this;

    protected function syncDatas(Request $request)
        $datas = (object) json_decode($request->datas);

        return [
            'name' => $datas->name,
            'type' => $datas->type,
            'schedule' => [
                'start' => $datas->date_start,
                'end' => $datas->date_end
            'members' => collect($datas->client->members)->pluck('id'),
            'price' => $datas->price

    protected function syncRelations(Request $request)
        $datas = (object) json_decode($request->datas);


As you see, I inject the request object and format data before store it with the syncDatas() method and do to the same for the relations.

This is mainly the reason why I think about Services/Repositories pattern because I don’t know if that is a good way to use Model at this step. But i’m not really convinced about using those patterns into Laravel and as many, want to keep my Controllers very clean and easy to read.

So, I want to know, what is your approach on this case ? Do you use Services/Repositories pattern in your projects ?

Please sign in or create an account to participate in this conversation.