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 ?
@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
@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.
@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)
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.