Shawdow's avatar

How to Create a table dynamically in the controller??

I don't know whether the table or column can be added in the laravel controller??

Question arises because in the ecommerce website i need to create a properties table for a particular Product.

Suppose Product Properties can be created dynamically it could easy to and the Properties for a Particular Product in the view.

any solution regarding to above??

Thanks,

0 likes
12 replies
martinbean's avatar

@Shawdow Don’t. Your database schema should be version-controlled. Properly model your data instead.

Snapey's avatar

how are you storing these extra attributes?

Shawdow's avatar

@Snapey I am Starting to do the Properties for a Particular Product so thought that it is better to add the Properties for a Particular Product.this can be done in the laravel ??

Snapey's avatar

well, you cannot have a products table that has different columns for different types of product

Shawdow's avatar

@Snapey

below is my Product table i need to add the Properties/attributes for every Product because in the ecommerce site Properties may be different

for example if the Product is Book it can have author name,date of publish etc.. but if the Product is mobile it can have color,memory etc.. so i came up with the solution that in the controller if we create a table it could easy to implement. I hope it understands!!

+------------------+--------------+------+-----+---------+----------------+
| Field            | Type         | Null | Key | Default | Extra          |
+------------------+--------------+------+-----+---------+----------------+
| product_id       | int(100)     | NO   | PRI | NULL    | auto_increment |
| product_cat      | int(100)     | NO   |     | NULL    |                |
| product_brand    | int(100)     | NO   |     | NULL    |                |
| product_title    | varchar(255) | NO   |     | NULL    |                |
| product_price    | int(100)     | NO   |     | NULL    |                |
| product_desc     | text         | NO   |     | NULL    |                |
| product_image    | text         | NO   |     | NULL    |                |
| product_keywords | text         | NO   |     | NULL    |                |
+------------------+--------------+------+-----+---------+----------------+
1 like
Shawdow's avatar

@Snapey

I have reffered your link but i don't understand table name given !!!

Schema::create(config('rinvex.attributes.tables.attributes'), function (Blueprint $table) {
            // Columns
            $table->increments('id');
            $table->string('slug');
            $table->{$this->jsonable()}('name');
            $table->{$this->jsonable()}('description')->nullable();
            $table->mediumInteger('sort_order')->unsigned()->default(0);
            $table->string('group')->nullable();
            $table->string('type');
            $table->boolean('is_required')->default(false);
            $table->boolean('is_collection')->default(false);
            $table->text('default')->nullable();
            $table->timestamps();
        });
1 like
Snapey's avatar

its saying that the table name comes from a config value

Shawdow's avatar

@Snapey yeah I got it but this is not what i want can Laravel creates table/column in Controller Suppose if the admin adds the Properties like color for Mobile laravel should automatically create a column color in the table is there any options to achieve it??

Snapey's avatar

no, use a package like suggested

insight's avatar

Dear @shawdow ,

You can use the below sample code

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;

class TableController extends Controller
{
    public function createTable()
    {
        Schema::create('customers', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email');
            $table->string('phone');
            $table->timestamps();
        });
    }
}

Thanks

Anes P A

Please or to participate in this conversation.