daek1312 / database
初始化 illuminate/database 包
v0.7.3
2017-05-08 16:19 UTC
Requires
- davek1312/app: 0.2.*
- davek1312/config: 0.3.*
- davek1312/console: 0.8.*
- davek1312/validation: 0.0.*
- illuminate/database: ^5.4
- illuminate/events: ^5.4
- illuminate/filesystem: ^5.4
Requires (Dev)
- phpunit/phpunit: ~4.0
README
初始化 illuminate/database 包。
安装
此包可在 Packagist 上找到,您可以使用 Composer 进行安装。
composer require davek1312/database
配置
将 vendor\davek1312\database\davek1312 复制到您的应用程序根目录。
数据库连接
将您的数据库连接添加到应用程序根目录中的 davek1312\database\config\database.php 文件中的 connections 数组。
<?php
return [
// Default connection that will be used
'default' => davek1312_env('DATABASE_DEFAULT', 'default'),
// Migrations table name
'migrations' => davek1312_env('DATABASE_MIGRATIONS', 'migrations'),
'connections' => [
'example_connection' => [
'driver' => 'mysql',
'host' => davek1312_env('EXAMPLE_CONNECTION_HOST', 'default_database_host'),
'port' => davek1312_env('EXAMPLE_CONNECTION_PORT', 'default_database_port'),
'database' => davek1312_env('EXAMPLE_CONNECTION_DATABASE', 'default_database_name'),
'username' => davek1312_env('EXAMPLE_CONNECTION_USERNAME', 'default_database_username'),
'password' => davek1312_env('EXAMPLE_CONNECTION_PASSWORD', 'default_database_password'),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
],
];
Eloquent 使用
您可以在 Laravel 上查看 Eloquent 文档。
迁移
创建迁移
vendor\bin\davek1312-console migration:make name --path= --table= --create
注册迁移
要注册迁移,请查看 davek1312\app 文档。
运行迁移
vendor\bin\davek1312-console migration:migrate
回滚迁移
vendor\bin\davek1312-console migration:rollback
重置数据库
vendor\bin\davek1312-console migration:reset
验证
在保存/更新模型时,您可以对 Eloquent 模型进行验证。您可以选择扩展我们的 Eloquent 模型版本或使用我们的模型验证特性。
扩展模型
<?php
namespace YourNamespace;
class YourModel extends \Davek1312\Database\Model {
}
使用验证特性
<?php
namespace YourNamespace;
class YourModel extends \Illuminate\Database\Eloquent\Model {
use \Davek1312\Database\Traits\ModelValidation;
}
模型验证配置
您可以通过查看 valdation 文档来查看可用的规则以及如何实现自定义消息。
<?php
namespace YourNamespace;
class YourModel extends \Davek1312\Database\Model {
/**
* @var array
*/
protected $fillable = ['name'];
/**
* @var array
*/
protected $validationRules = ['name' => 'required'];
/**
* The message for any customer validation rules specific to the model.
*
* @var array
*/
protected $customValidationMessages = [];
/**
* The locale for the validation messages. Will default to the app's locale.
*
* @var string
*/
protected $validationLocale;
}
验证方法
<?php
$model = new YourModel($data);
// Will only save if isValid() returns true. Applies to saving/updating/creating events.
$model->save();
// Checks if the model passes validation
$model->isValid();
//Returns all the validation messages for failed rules
$model->getValidationMessages();