如何在 Laravel 5 中创建 table 迁移
How to create table migration in Laravel 5
我正在从事一个项目,我被分配了一项任务来为应用程序创建用户管理。但我一直坚持 table 关系和他们的迁移。
努力
我有这些 table:
- 用户
- user_id
- 用户名
- 密码
- 个人资料
- profile_id
- user_id
- 名字
- 姓氏
- 电子邮件
地址
- address_id
- profile_id
- 地址
- 城市
- 状态
- 国家
- 密码
- 配置
- config_id
- configuration_name
- configuration_type
- parent_id
现在我必须为上面相同的结构创建模型和迁移。为此,我有 create/modify 下面的模型和迁移 class。
型号:用户
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'username', 'password',
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function profile()
{
return $this->hasOne('Profile','user_id');
}
}
迁移:2014_10_12_000000_create_users_table.php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('user_id');
$table->string('username');
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
}
型号:简介
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
public function user(){
return $this->belongsTo('User');
}
public function address()
{
return $this->hasOne('Address','address_id');
}
}
迁移:2016_02_26_101749_create_profiles_table.php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProfilesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->increments('profile_id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('user_id')->on('users')->onDelete('cascade');
$table->string('lastname')->nullable();
$table->string('firstname')->nullable();
$table->string('gender')->nullable();
$table->string('email')->unique();
$table->string('phonenumber', 20)->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('profiles');
}
}
型号:地址
namespace App;
use Illuminate\Database\Eloquent\Model;
class Address extends Model
{
public function profile(){
return $this->belongsTo('Profile');
}
public function city() {
return $this->hasOne('Configuration', 'config_id');
}
public function state() {
return $this->hasOne('Configuration', 'config_id');
}
public function country() {
return $this->hasOne('Configuration', 'config_id');
}
}
迁移:2016_02_26_102805_create_addresses_table.php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAddressesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('addresses', function (Blueprint $table) {
$table->increments('address_id');
$table->integer('profile_id')->unsigned();
$table->foreign('profile_id')->references('profile_id')->on('profiles')->onDelete('cascade');
$table->string('address')->nullable();
$table->integer('city')->unsigned();
$table->foreign('city')->references('config_id')->on('configurations')->onDelete('cascade');
$table->string('pincode')->nullable();
$table->integer('state')->unsigned();
$table->foreign('state')->references('config_id')->on('configurations')->onDelete('cascade');
$table->integer('country')->unsigned();
$table->foreign('country')->references('config_id')->on('configurations')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('addresses');
}
}
型号:配置
namespace App;
use Illuminate\Database\Eloquent\Model;
class Configuration extends Model
{
public function children() {
return $this->hasMany('Configuration','parent_id');
}
public function parent() {
return $this->belongsTo('Configuration','parent_id');
}
public function address(){
return $this->belongsTo('Address');
}
}
迁移:2016_02_26_104519_create_configurations_table.php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateConfigurationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('configurations', function (Blueprint $table) {
$table->increments('config_id');
$table->string('configuration_name');
$table->string('configuration_type');
$table->string('parent_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('configurations');
}
}
现在,当我 运行 php artisan migrate
我收到错误消息:
.
请建议我该怎么做。我必须使用相同的 table 结构并且不能修改它。如果需要任何进一步的更新或者我忘记了什么,请告诉我。
发生这种情况是因为迁移将尝试在配置之前迁移地址 table,因此它不会找到您引用的外键 config_id
,因此您可以更改迁移文件的名称然后migration
commad 可以先传递 configurations_table
迁移文件,然后再传递 addresses_table
迁移文件,所以只需更改:
2016_02_26_104519_create_configurations_table.php
收件人:
2016_02_26_102005_create_configurations_table.php
_____________^
之后你应该 运行 优化命令来重新生成优化的 class 加载器:
php artisan o
重新运行php artisan migrate
命令现在问题应该解决了
希望对您有所帮助。
我正在从事一个项目,我被分配了一项任务来为应用程序创建用户管理。但我一直坚持 table 关系和他们的迁移。
努力
我有这些 table:
- 用户
- user_id
- 用户名
- 密码
- 个人资料
- profile_id
- user_id
- 名字
- 姓氏
- 电子邮件
地址
- address_id
- profile_id
- 地址
- 城市
- 状态
- 国家
- 密码
- 配置
- config_id
- configuration_name
- configuration_type
- parent_id
现在我必须为上面相同的结构创建模型和迁移。为此,我有 create/modify 下面的模型和迁移 class。
型号:用户
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'username', 'password',
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function profile()
{
return $this->hasOne('Profile','user_id');
}
}
迁移:2014_10_12_000000_create_users_table.php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('user_id');
$table->string('username');
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
}
型号:简介
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
public function user(){
return $this->belongsTo('User');
}
public function address()
{
return $this->hasOne('Address','address_id');
}
}
迁移:2016_02_26_101749_create_profiles_table.php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProfilesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->increments('profile_id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('user_id')->on('users')->onDelete('cascade');
$table->string('lastname')->nullable();
$table->string('firstname')->nullable();
$table->string('gender')->nullable();
$table->string('email')->unique();
$table->string('phonenumber', 20)->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('profiles');
}
}
型号:地址
namespace App;
use Illuminate\Database\Eloquent\Model;
class Address extends Model
{
public function profile(){
return $this->belongsTo('Profile');
}
public function city() {
return $this->hasOne('Configuration', 'config_id');
}
public function state() {
return $this->hasOne('Configuration', 'config_id');
}
public function country() {
return $this->hasOne('Configuration', 'config_id');
}
}
迁移:2016_02_26_102805_create_addresses_table.php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAddressesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('addresses', function (Blueprint $table) {
$table->increments('address_id');
$table->integer('profile_id')->unsigned();
$table->foreign('profile_id')->references('profile_id')->on('profiles')->onDelete('cascade');
$table->string('address')->nullable();
$table->integer('city')->unsigned();
$table->foreign('city')->references('config_id')->on('configurations')->onDelete('cascade');
$table->string('pincode')->nullable();
$table->integer('state')->unsigned();
$table->foreign('state')->references('config_id')->on('configurations')->onDelete('cascade');
$table->integer('country')->unsigned();
$table->foreign('country')->references('config_id')->on('configurations')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('addresses');
}
}
型号:配置
namespace App;
use Illuminate\Database\Eloquent\Model;
class Configuration extends Model
{
public function children() {
return $this->hasMany('Configuration','parent_id');
}
public function parent() {
return $this->belongsTo('Configuration','parent_id');
}
public function address(){
return $this->belongsTo('Address');
}
}
迁移:2016_02_26_104519_create_configurations_table.php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateConfigurationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('configurations', function (Blueprint $table) {
$table->increments('config_id');
$table->string('configuration_name');
$table->string('configuration_type');
$table->string('parent_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('configurations');
}
}
现在,当我 运行 php artisan migrate
我收到错误消息:
请建议我该怎么做。我必须使用相同的 table 结构并且不能修改它。如果需要任何进一步的更新或者我忘记了什么,请告诉我。
发生这种情况是因为迁移将尝试在配置之前迁移地址 table,因此它不会找到您引用的外键 config_id
,因此您可以更改迁移文件的名称然后migration
commad 可以先传递 configurations_table
迁移文件,然后再传递 addresses_table
迁移文件,所以只需更改:
2016_02_26_104519_create_configurations_table.php
收件人:
2016_02_26_102005_create_configurations_table.php
_____________^
之后你应该 运行 优化命令来重新生成优化的 class 加载器:
php artisan o
重新运行php artisan migrate
命令现在问题应该解决了
希望对您有所帮助。