简体   繁体   中英

Add new column dynamically in table mySQL with Laravel

I have a sensor list form in my program. There the user can do CRUD. How do when the user enters new sensor data, the new data automatically creates a new column in the table in my database (automatically alter table)

public function store(Request $request){
   $data = new Mst_sensor();
   $arr = array_merge($arr, ['created_by' => Auth::user()->userid, 'created_at' => date('Y-m-d H:i:s')]);
   $data->create($arr);
   

   $sensor= new Trs_sensor_d;
   $table = $sensor->getTable();
   $columns = \DB::getSchemaBuilder()->getColumnListing($table);
        
   $data_field = [];
   foreach ($columns as $k => $v) {
      $data_field[$k] = $v;
   }
   $removed = array_pop($data_field);
   $fix_data_field = end($data_field);

   $q = "ALTER TABLE `trs_raw_d_gpa` ADD `coba` VARCHAR(25) NULL AFTER '" . $fix_data_field . "'";
}

How can alter table run automatically???

Think about how to design the database so that users cannot change the structure. What you want to do is not recommended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM