Laravel Laravel 5.5

How to create dynamic table with dynamic fields in Laravel 5

create dynamic table with dynamic fields in Laravel 5
Written by Yogesh Koli

Introduction:

In this tutorial I am going to provide a solution on how to create dynamic database tables along with dynamic fields in Laravel 5 project without writing a single SQL query. if you are Laravel developer and used migrations before you must pretty much familiar with this approach, Laravel migrations are incredibly simple and quicker way to interact with database tables here we will use same solution but run time and by that mean it can be a anything where you need dynamic table created in backend according to user need.

This solution can be applied to form builder projects backend, just like google forms where user can create dynamic forms so you always needs to deal with dynamic fields for the tables fields could be anything and any count so this eventually this tutorial will help you to handle backend operation of form builder.

Okay let’s dive into the solution, first think you need to be ready with Laravel project along with database configuration setting ready and yes you can use your existing project as well.

Solution:

If you have Laravel 5 project up and running then execute following command to create new controller into your project, we will use this controller to perform table creation operations:

Add following dependencies at top of the controller:

Add this method into TableController, this is the method which is going to handle create operation for the table, you just needs to provide your table name and dynamic fields:

/app/Http/Controllers/TableController.php:

and now you can easily call above method like example showing below:

Note: The above example is just simple for the demonstration you can update this example according to your needs and make fields dynamic and also we are setting up id as primary key auto increment for the table.

To remove the table from database use following method:

Testing:

To test above solution I have created simple route to access operate() method from the controller showing below:

/routes/web.php:

Now we can access /create-table route and see what comes into place.

Here is the Result:

And checkout the table from database:

Table Details

Table Details

 

We are done.

If you find this solution helpful or have any question in mind let me know using comment box below.

About the author

Yogesh Koli

Yogesh Koli is a software engineer & a Blogger lives in India. He's driven by an addiction to learning and a love for adventure. he has 5+ years of experience working with the front-end, back-end, web application development, and system design.

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of