我的Models结构是在app目录下建立了Models/cms和Models/api目录。
1、新建model
User.php
Role.php
RoleUser.php
Permission.php
PermissionRole.php
Menu.php
is_hide == 0) {
if ($menu->child) {
$html .= ''
. ' ' . $menu->name . ''
. ' '
. ' ';
} else {
$html .= ' ' . $menu->name . ' ';
}
}
}
return $html;
}
}
Tree.php
parent_id == $parent_id) {
if ($level != 0) {
$model->html = str_repeat(" ", $level);
$model->html .= '|';
}
$model->html .= str_repeat($html, $level);
$tree[] = $model;
$tree = array_merge($tree, self::createLevelTree($models, $model->id, $level + 1));
}
}
return $tree;
}
/**
* 生成无序节点树
*
* @param $models
* @param int $parent_id
* @param string $node
*
* @return array
*/
public static function createNodeTree($models, $parent_id = 0, $node = 'child')
{
$tree = [];
foreach ($models as $model) {
if ($model->parent_id == $parent_id) {
$model->$node = self::createNodeTree($models, $model->id);
$tree[] = $model;
}
}
return $tree;
}
}
以上是一堆model的建立。
2、新建数据库迁移文件migrations
修改2014_10_12_000000_create_users_table.php
increments('id');
$table->string('name', 32);
$table->string('email', 128);
$table->string('password', 64);
$table->rememberToken();
$table->timestamps();
$table->unique('email', 'users_email_unique');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
注意laravel 5.4 改变了默认的数据库字符集,现在utf8mb4包括存储emojis支持。如果你运行MySQL v5.7.7或者更高版本,则不需要做任何事情。
当你试着在一些MariaDB或者一些老版本的的MySQL上运行 migrations 命令时,你可能会碰到下面这个错误:
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table users add unique users_email_unique(email))
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
解决方法是修改:\app\Providers\AppServiceProvider.php文件:
use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider {
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
2017_06_14_100454_create_menus_table.php
string('email')->index();
$table->string('token');
$table->timestamp('created_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('password_resets');
}
}
2017_06_14_134742_entrust_setup_tables.php
increments('id');
$table->string('name')->unique();
$table->string('display_name')->nullable();
$table->string('description')->nullable();
$table->timestamps();
});
// Create table for associating roles to users (Many-to-Many)
Schema::create('role_user', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles')
->onUpdate('cascade')->onDelete('cascade');
$table->primary(['user_id', 'role_id']);
});
// Create table for storing permissions
Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('display_name')->nullable();
$table->timestamps();
});
// Create table for associating permissions to roles (Many-to-Many)
Schema::create('permission_role', function (Blueprint $table) {
$table->integer('permission_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->foreign('permission_id')->references('id')->on('permissions')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles')
->onUpdate('cascade')->onDelete('cascade');
$table->primary(['permission_id', 'role_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('permission_role');
Schema::drop('permissions');
Schema::drop('role_user');
Schema::drop('roles');
}
}
以上是数据库迁移文件的建立。
3、修改数据库填充文件
\database\seeds\DatabaseSeeder.php
这里的内容要根据实际应用需要建立,我只写了基础的部分,能登录和看首页:
call(UserTableSeeder::class);
$this->call(MenuTableSeeder::class);
$this->call(RoleTableSeeder::class);
$this->call(PermissionTableSeeder::class);
$this->call(RoleUserTableSeeder::class);
$this->call(PermissionRoleTableSeeder::class);
Model::reguard();
}
}
class UserTableSeeder extends Seeder
{
public function run()
{
DB::table("users")->delete();
User::create(["name" => "admin", "email" => "[email protected]", "password" => bcrypt(123456)]);
}
}
class MenuTableSeeder extends Seeder
{
public function run()
{
DB::table("menus")->delete();
Menu::create(["parent_id" => "0", "name" => "首页管理", "url" => "index", "description" => "展示系统的各项基础数据"]); //id:1
}
}
class RoleTableSeeder extends Seeder
{
public function run()
{
DB::table("roles")->delete();
Role::create(["name" => "admin", "display_name" => "User Administrator", "description" => "User is allowed to manage and edit other users"]);
Role::create(["name" => "owner", "display_name" => "Project Owner", "description" => "User is the owner of a given project"]);
}
}
class PermissionTableSeeder extends Seeder
{
public function run()
{
DB::table("permissions")->delete();
Permission::create(["display_name" => "首页管理", "name" => "index"]); // id:1
}
}
class RoleUserTableSeeder extends Seeder
{
public function run()
{
DB::table("role_user")->delete();
RoleUser::create(["user_id" => 1, "role_id" => 1]);
}
}
class PermissionRoleTableSeeder extends Seeder
{
public function run()
{
DB::table("permission_role")->delete();
/**
* 权限role
*/
for ($role = 1; $role <= 2; $role++) {
for ($permission = 1; $permission <= 1; $permission++) {
PermissionRole::create(["permission_id" => $permission, "role_id" => $role]);
}
}
}
}
4、配置数据库
(1)去数据库新建数据库,这个不多说了,建个空库
(2)然后去.env配置数据库访问信息
DB_CONNECTION=mysql
DB_HOST=
DB_PORT=3306
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
5、运行迁移和填充
php artisan migrate:install
php artisan migrate
php artisan db:seed
执行都没有问题,就可以在数据库看见相应的表了。
如果执行有问题,解决完问题后,可以回滚重建数据表和填充数据:
php artisan migrate:refresh php artisan db:seed