1. Go to this page and download the library: Download jukit/sharding-enhancer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jukit / sharding-enhancer example snippets
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Jukit\ShardingEnhancer\EnhanceModel;
use Jukit\ShardingEnhancer\Positioner;
class MailSharding extends EnhanceModel
{
use HasFactory, SoftDeletes;
use Positioner;
/**
* 可以批量赋值的属性
*
* @var array
*/
protected $fillable = [
'id', 'field_a', 'field_b' ...
];
/**
* @desc 获取表名
* @return string
*/
public function getTable(): string
{
return $this->table ?? env('DB_PREFIX', '') . 'mail';
}
public function getShardMaxCount(): int
{
return 64;
}
// 分表配置
public function getShardConfig(): array
{
return [
[
'partition' => 32,
'low' => 0,
'high' => 31,
],
[
'partition' => 64,
'low' => 32,
'high' => 63,
],
];
}
}
namespace App\Repositories;
use App\Models\MailSharding;
use Illuminate\Database\Eloquent\Model;
use Jukit\ShardingEnhancer\CrudShardingRepository;
class MailShardingRepository extends CrudShardingRepository
{
public function __construct(MailSharding $mail)
{
// 用于计算分表的值
$factor = 31;
$mail->setFactor($factor)->sharding();
$this->setModel($mail);
}
public function selectById(int $id): ?Model
{
return $this->getModel()->newQuery()->select('*')->where('id', $id)->first();
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Jukit\ShardingEnhancer\EnhanceModel;
use Jukit\ShardingEnhancer\Positioner;
class MailSharding extends EnhanceModel
{
use HasFactory, SoftDeletes;
use Positioner;
/**
* 可以批量赋值的属性
*
* @var array
*/
protected $fillable = [
'id', 'created_at', 'updated_at', 'deleted_at',
];
/**
* @desc 获取表名
* @return string
*/
public function getTable(): string
{
return $this->table ?? env('DB_PREFIX', '') . 'mail';
}
}
namespace App\Repositories;
use App\Models\Mail;
use Illuminate\Database\Eloquent\Model;
use Jukit\ShardingEnhancer\CrudShardingRepository;
class MailRepository extends CrudShardingRepository
{
public function __construct(Mail $mail)
{
$this->setModel($mail);
}
public function selectById(int $id): ?Model
{
return $this->getModel()->newQuery()->select('*')->where('id', $id)->first();
}
}
use App\Models\Mail;
use App\Repositories\MailRepository;
$mailRepo = new MailRepository(new Mail());
$mailId = 1;
$mail = $mailRepo->selectById($mailId);
dump($mail?->toArray());
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.