在迁移文件中获取 table id (laravel 5.8)
get table id in migration file (laravel 5.8)
我想要用户的ID。 (在迁移中使用 if 语句中的 id 并将不同的默认值添加到新的 table 列)
我尝试使用 getColumns()->id
但它在 ide 中显示了一些警告(未找到字段 ID)。
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddUserRoles extends Migration
{
public function up()
{
Schema::table('users', function (Blueprint $table) {
$ID = $table->getColumns()->id;
if($ID == '1') {
$table->string('role')->default('admin');
} else {
$table->string('role')->default('member');
});
}
public function down()
{
Schema::table...
.....
}
}
编辑:这是用户 table
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
我的目的是使用 id 为用户设置不同的默认角色 table。
您无法在创建用户时读取用户 table,因为您还没有任何用户。
为以下用户创建 table:
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->string('role')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
}
然后,您可以添加用户并勾选 id
为他们分配角色:
$user = new User();
$user->name = 'name';
$user->email = 'email@example.com';
$user->password = Hash::make("123456");
$user->save();
if ($user->id === $someId) {
$user->role = 'some_role';
$user->save();
}
希望对您有所帮助。
我想要用户的ID。 (在迁移中使用 if 语句中的 id 并将不同的默认值添加到新的 table 列)
我尝试使用 getColumns()->id
但它在 ide 中显示了一些警告(未找到字段 ID)。
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddUserRoles extends Migration
{
public function up()
{
Schema::table('users', function (Blueprint $table) {
$ID = $table->getColumns()->id;
if($ID == '1') {
$table->string('role')->default('admin');
} else {
$table->string('role')->default('member');
});
}
public function down()
{
Schema::table...
.....
}
}
编辑:这是用户 table
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
我的目的是使用 id 为用户设置不同的默认角色 table。
您无法在创建用户时读取用户 table,因为您还没有任何用户。
为以下用户创建 table:
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->string('role')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
}
然后,您可以添加用户并勾选 id
为他们分配角色:
$user = new User();
$user->name = 'name';
$user->email = 'email@example.com';
$user->password = Hash::make("123456");
$user->save();
if ($user->id === $someId) {
$user->role = 'some_role';
$user->save();
}
希望对您有所帮助。