laravel 自帶模板漢化
laravel自帶的auth模板是英文的。有大神開發了全套英文漢化ServiceProvier。
安裝語言包:
$ composer require "overtrue/laravel-lang:~3.0"
在config/app.php中的providers
陣列中:
Illuminate\Translation\TranslationServiceProvider::class,
替換為:
Overtrue\LaravelLang\TranslationServiceProvider::class,
完成後,重新整理,看到英文已經被翻譯好了。
多資料庫配置
考慮到後期的分庫的問題,摸索了一下多資料庫模式的操作。
###.env配置
把原來的DB_CONNECTION=mysql這段複製一份,改成其他的資料庫配置,如下程式碼所示:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=flower
DB_USERNAME=homestead
DB_PASSWORD=secret
DB_HOST_BBS=127.0.0.1
DB_PORT_BBS=3306
DB_DATABASE_BBS=bbs
DB_USERNAME_BBS=homestead
DB_PASSWORD_BBS=secret
然後在config/database中的connections
陣列中新增配置:
'mysql_bbs' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST_BBS', '127.0.0.1'),
'port' => env('DB_PORT_BBS', '3306'),
'database' => env('DB_DATABASE_BBS', 'forge'),
'username' => env('DB_USERNAME_BBS', 'forge'),
'password' => env('DB_PASSWORD_BBS', ''),
'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'),
]) : [],
],
migration
migration部分,這樣配置:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTestsTable extends Migration
{
protected $connection = 'mysql_bbs';
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::connection('mysql_bbs')->create('tests', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tests');
}
}
model
我們執行php artisan migrate
遷移以上程式碼中的migration配置。然後,建立一個model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Test extends Model
{
//
protected $connection = 'mysql_bbs';
}
最後,我們在tinker中做一下測試。先執行php artisan tinker
進入互動模式,然後執行:
>>> App\Test::create();
=> App\Test {#4083
updated_at: "2020-09-14 18:42:46",
created_at: "2020-09-14 18:42:46",
id: 2,
}
>>>
插入成功。在bbs資料庫中可以看到新增加的資料行了:
本作品採用《CC 協議》,轉載必須註明作者和本文連結