每一个你不满意的现在,都有一个你不努力的曾经。

分表


<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ChapterPayLog extends Model
{
    /**
     * 模型的连接名称
     *
     * @var string
     */
    protected $connection = 'story_app_log';

    protected $tableName = "chapter_pay_logs_";

    protected $fillable = [
        'user_id', 'book_id', 'chapter_id', 'price','number','channel','openid'
    ];

    /**
     * 处理分表名
     * @param $user_id
     * @return $this|ChapterPayLog
     */
    public function handleTable($user_id)
    {
        $tableNumber = $user_id % 10;

        return $this->setTable($this->tableName . $tableNumber);
    }

}

env('DB_DATABASE_LOG') => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE_LOG', 'forge'),
            'username' => env('DB_USERNAME_LOG', 'forge'),
            'password' => env('DB_PASSWORD_LOG', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

DB_DATABASE_LOG=story_app_log
DB_USERNAME_LOG=root
DB_PASSWORD_LOG=root

(new ChapterPayLog())->handleTable($user->id)->newQuery()->create([
    'user_id'   =>  $user->id,
    'book_id'   =>  $book->id,
    'chapter_id'    =>  $chapter->id,
    'number'        =>  $chapter->number,
    'price'         =>  $book->coin,
    'channel'       =>  $book->channel,
    'openid'        =>  $book->openid
]);
Card image cap

每一个你不满意的现在,都有一个你不努力的曾经。