欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Laravel獲取所有的數(shù)據(jù)庫表及結(jié)構(gòu)的方法

 更新時(shí)間:2019年10月10日 17:00:13   作者:SHUIPING_YANG  
今天小編就為大家分享一篇Laravel獲取所有的數(shù)據(jù)庫表及結(jié)構(gòu)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

遇到一個(gè)需求,需要修改數(shù)據(jù)庫中所有包含email的字段的表,要把里面的長(zhǎng)度改為128位。Laravel獲取所有的表,然后循環(huán)判斷表里面有沒有email這個(gè)字段。

代碼如下:

use Illuminate\Support\Facades\Schema;
use DB;

public function getDatabaseColumns() {
 $tables = DB::select('show tables');
 $tables = array_column($tables, 'Tables_in_new_bcc_web');
 $columns = ['email', 'user_name', 'nick_name', 'first_name', 'last_name'];
 // dd(Schema::getConnection());
 foreach ($tables as $key => $value) {
  foreach ($columns as $k => $v) {
   if (Schema::hasColumn($value, $v)) {
    $table[] = $value;
   };
  }
  // $columns[] = Schema::getColumnListing('users');
 }
 $table = array_unique($table);
 dd($table);
}
Schema::getColumnListing('user');
Schema::hasColumn($table, $column_name)

這里記一筆,比知道有沒有更好的方法一步獲取到當(dāng)前連接的數(shù)據(jù)庫里面的所有的表,我是用原生的sql語句show tables查出所有表,然后取出Tables_in_new_bcc_web這一列,然后才得到所有的表名,然后再去循環(huán)。

找到一個(gè)更棒的方式:

public function getDatabaseColumns() {
 $tables = array_map('reset', \DB::select('SHOW TABLES'));
 $columns = ['email', 'user_name', 'nick_name', 'first_name', 'last_name'];
 foreach ($tables as $key => $value) {
  foreach ($columns as $k => $v) {
   if (Schema::hasColumn($value, $v)) {
    $table[] = $value;
   };
  }
 }
 $table = array_unique($table);
 dd($table);
}

以上這篇Laravel獲取所有的數(shù)據(jù)庫表及結(jié)構(gòu)的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論