(舉例)Laravel 怎麼分表

php迷途小書童發表於2019-08-14
<?php
namespace App\Models;

use DB;
use Schema;
use Illuminate\Database\Eloquent\Model;

class Logs extends Model
{
    protected $guarded = ['id'];
    protected $table;

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->table = 'log_'.date('Y');
        if (!Schema::hasTable($this->table))
        {
            DB::update('create table '.$this->table.' like logs');
        }
    }
}
  • 前提是你有logs這個表,這樣才能複製表
Logs::where('uid','')->first();

相關文章