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

swapnil__weeb's avatar

How to use single component for entire crud in laravel livewire ?

I am learning livewire but the problem I am facing is. When I create the user crud it requires component for each operation add edit update and so on. That's totally fine for blade files. Coz they are seperate but I don't want to create 4 functions for crud for 4 components. I want to create one master component for user in this class file I will use those crud methods. Is there any way to do so ?

0 likes
8 replies
tisuchi's avatar

@swapnil__weeb Yes, you can absolutely create a single Livewire component to handle all CRUD operations for a resource like User in Laravel.

This approach is not only efficient but also promotes code reusability and organization.

1 like
Chingy's avatar

@tisuchi He can but shouldn't be encouraged. A single component can become quickly bloated with stuff that are needed to considered for each crud. No need to be doing if (URL::route) and unecessary if conditions that lower the code's quality

tisuchi's avatar

@Chingy its a trade-off.

Sometimes glue code (dumping eventing in one class/controller) makes more sense than a fancy/tight architectural code!

A single component can become quickly bloated with stuff...

Fully agree, which does not mean that you need to be always fancy in terms of the architecture of your code. Again it's subjective of it's use-case.

1 like
swapnil__weeb's avatar

@tisuchi I guess so sir. But we have to follow the architecture and want to maintain the code quality.

1 like
Chingy's avatar

Highly suggest to split each crud operation to a separate component. Better for maintenance and extending your code in the future

1 like
swapnil__weeb's avatar

@Chingy Yes but it will create more and more files. 4 compoents and blade files for crud operation. 4 vws are fine but 4 components sounds like lot of files of code.

Chingy's avatar

@swapnil__weeb I usually do a Create, an Edit and an Index.

Create a trait that includes the most core logic there, such as create, update and delete. For the index, i do a computed property in the trait but I will load them only in index , which will fire an wire:init, make a variable true (such as loadItems()) and the computed property won't load for views that you dont need to (create and edit)

Snapey's avatar

I say its fine, and do it often.

The create and edit views are usually identical, then all you need is a delete button.

I would usually create an index page which is a simple blade file, then when you click the user record, open the livewire view to update that user, or have a 'new user' button which uses the same component but passes in an empty or new user model.

Validation is mostly the same, and it needs a small test on save or create when saving to the model.

Please or to participate in this conversation.