laravel 自动分表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_39967349/article/details/84849110

这里是按照每天去创建一个表代码稍显捡漏

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redis;
use Carbon\Carbon;
use Illuminate\Support\Facades\Schema;
public function index(){
    $tables = DB::select('SHOW TABLES');
    $object =  json_decode( json_encode( $tables),true);
    $names = array();
    foreach($object as $k => $v){
        $names[] = $v['Tables_in_shop'];
    }
    $time = 'sp_ad'.Carbon::now()->toDateString();
    $isin = in_array($time,$names);
    if($isin){
        echo '已存在';
    }else{
        echo "不存在";
        Schema::create($time, function ($table) {
            $table->increments('id');
            $table->char('adname',45);
        });
    }
    DB::table($time)->insert(
        [
            'adname'=>'daze',
        ]
    );
}

猜你喜欢

转载自blog.csdn.net/weixin_39967349/article/details/84849110