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

ToxifiedM's avatar

To carry out CRUD operations for multiple tables in a single view

I'm on Laravel 8 with Livewire, currently have 3 models, Category, SubCategory and MenuItem for 3 tables. All the above models have separate livewire controllers and have the code for the CRUD operations respectively. I have separate views and routes to edit the above tables and they all have a eloquent relationship between each other. Now what I need to do here to is, I need to display all the three tables in a single view to carry out the CRUD operations. Please help me with this. Thanks a lot for your time!

0 likes
5 replies
Wakanda's avatar

@toxifiedm create instances of all the 3 models in your controller or livewire class then echo that data in your view in a tabular format. Then add buttons to edit and delete each instance and maybe a modal to create each model

ToxifiedM's avatar

@loyd Hey Loyd, thanks for the revert! So I have separate controllers for all the three models. So I have initialized several functions for different operations. Lets take an example for the store operation function.

public function store(){
        $this->validate(
            [   
                'sub_category_id' => 'required',
                'item_name' => 'required',
            ]
        );

        MenuItem::updateOrCreate(['id' => $this->itemId], [
            'sub_category_id' => $this->sub_category_id,
            'item_name' => $this->item_name,
            'item_description' => $this->item_description
        ]);

        $this->hideModal();

        session()->flash('info', $this->itemId ? 'Post Update Successfully' : 'Post Created Successfully' );

        $this->itemId = '';
        $this->sub_category_id = '';
        $this->item_name = '';
        $this->item_description = '';
    }

So what I need to ask here is, do I need to merge the code from the other two controllers for the above operation under the public function store() or I can initialize the store function for the other controllers with a different name?

Wakanda's avatar

@toxifiedm if all the operations are going to be coming from the same view you can just create different methods within the same class

ToxifiedM's avatar

Sure mate, thanks a lot! I'll give it a try and will get back to you.

ToxifiedM's avatar
ToxifiedM
OP
Best Answer
Level 1

I achieved the said above result by using the components feature in livewire, embedded the three components for three tables, without merging the controllers, clean and yet maintainable, thanks anyways :)

Please or to participate in this conversation.