Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

beyondelayer's avatar

Is my use of Laravel Repository-Service pattern correct?

Hello everyone, I'm new and I've been working on patterns for about 2 weeks and I've been implementing my applications. But I'm not sure about their accuracy, so I wanted the professionals here to see and comment. Please inform me if I'm wrong or help me optimize them. Because I will use it everywhere in my project and I need it very much. Thank you very much in advance.

I share it on github so you can examine it better: You can take the Store method as an example. https://github.com/beyondelayer/test

0 likes
10 replies
Snapey's avatar

You will find very few people here are fans of the repository pattern.

There is just too much overlap with functionality offered by Eloquent.

beyondelayer's avatar

@Snapey I get it, this might be the best news I've ever gotten because I've been trying to figure this out for days and as you say it's really complicated. So what would you suggest to me, by the way, thank you for the information.

The only thing I want is not to repeat code by typing the same update and store commands for everything (user, post, category, etc.).

Snapey's avatar
Snapey
Best Answer
Level 122

@beyondelayer take the example below

public function store (UserRequest $request, User $user)
{
	$user->fill($request->validated());

    $user->save();

	return redirect('users.index')->with('success','User data saved');
}

Then think how this would be with repository pattern and service class, and how much code is duplicated.

1 like
beyondelayer's avatar

@Snapey It looks very good and the workload is light, but I have heard that batch assignments are harmful. when I fill or save, I have to write all the data in the fillable one by one. Does this cause a security weakness?

I usually add them one by one, like this:

$user->name = $request->input('name'); $user->password = $request->input('password');

Snapey's avatar

I'm only filling the model with data that has already been validated and only fields that have validation rules.

Therefore I am in complete control of what gets sent to the model.

If you write the data field by field then $fillable or $guarded are irrelevant because you are not doing mass assignment

Your approach is always valid, but especially so if the form field names do not match the model column names.

1 like
beyondelayer's avatar

@Snapey In other words, even if we write everything in fillable, the names we write in the request class are processed and attackers cannot reach the places where we write outside the request. This information is really gold for me, thank you very much. I got rid of tens of lines of code

Snapey's avatar

@beyondelayer I don't want to stop you learning about design patterns, just highlighting that repository pattern is, in my opinion, an over-complication in Laravel apps

Please or to participate in this conversation.