Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to dynamically create column of table in Laravel 5.4.
I know that I can modify tables in Laravel through migration.
For example,
table1 is included following columns: id, productname.
When I insert valuse into table1(id:1, productname:bag), I want to dynamically create column named "bag" in table2.

Please help me.

What I have tried:

I created table1, table2 into mysql.
Posted
Updated 16-Aug-17 21:55pm

1 solution

I solved this problem.
If name of table that I'm going to get names of columns and data is 'XXX',
first, I wrote following as code at controller.

public function index(){
....
$columns = DB::select('show columns from XXX);
$contents = DB::table('XXX')->get();
$contents_keys = array_keys($contents->toArray());
foreach($columns as col) {
$contents_data[] = $col->Filed;
}
....
return view('route of view')->with('columns', $columns)
->with('contents_data', $contents_data)
->with('contents', $contents)
->with('contens_keys', $content_keys);
}

and then I wrote code at xx.blade.php on View.

...



@foreach($columns as $column)



@foreach($contents as $item)

@foreach($contents_data as $key)

@endforeach

@endforeach

{{$column->Field}} @endif
{{ $item->$key }}

....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900