I spend lots of time to solve this error but it won't, I just delete migrate and recreate, lots of time drop phpmyadmin database row, composer autoload but I am not able to solve
When I try to migration:refresh or rollback this error happen
ErrorException Undefined index: 2017_05_14_071616_create_users_table
The code is as follows (create_users_table.php)
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
Schema::drop('users');
$table->increments('id');
$table->integer('role_id')->unsigned();
$table->string('name');
$table->string('email',50)->unique();
$table->string('username');
$table->string('password');
$table->boolean('active');
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::drop('users');
}
}
Related
When I do a migration by using the command php artisan migrate I have this error: In Macroable.php line 104: Method Illuminate\Database\Schema\Blueprint::id does not exist.
I don't understand where this error arises from.
Do you have an idea please?
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFilesTable extends Migration
{
public function up()
{
Schema::create('files', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->nullable();
$table->string('file_path')->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('files');
}
}
I am getting the following error when running migrations. The error in my terminal console is:
SQLSTATE[42000]: Syntax error or access violation: 1170 BLOB/TEXT column 'referral_code' used in key specification without a key length (SQL: alter table `users` add index `users_referral_code_index`(`referral_code`))
Below is my actual migration file. I can't see to tell why it's happening, as I've used this pattern before with other migrations.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddReferralInfoToUsersTable extends Migration
{
public function up()
{
Schema::table('shop_users', function (Blueprint $table) {
$table->text('referral_code')->nullable();
$table->integer('referral_uses')->nullable();
$table->integer('referral_revenue')->nullable();
$table->index(['referral_code']);
});
}
}
Try changing the datatype of referral_code to string and set a length.
Schema::table('shop_users', function (Blueprint $table) {
$table->string('referral_code',100)->nullable();
}
You can try this:
Schema::table('shop_users', function (Blueprint $table) {
$table->mediumText('referral_code')->nullable();
}
or
Schema::table('shop_users', function (Blueprint $table) {
$table->string('referral_code',100)->nullable();
}
or
Schema::table('shop_users', function (Blueprint $table) {
$table->longText('referral_code')->nullable();
}
I want id of the users. (in migrations to use the id in if statements and add different default values to new table column)
I tried to use getColumns()->id but it showing some warning in ide(Field id not found).
<?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...
.....
}
}
EDIT: this the users 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();
});
}
my purpose is to use id for setting different default roles to the users table.
You can't read users while creating the users table, since you does not have any users yet.
Create this table for users:
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();
});
}
}
Then, you can add users and check the id to assign them a role:
$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();
}
Hope it helps.
I'm trying to migrate my database to an empty database,
but I get the following error when I do php artisan migrate:
[Illuminate\Database\QueryException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'portal-for-fun.permissions' doesn't exist (SQL: select *
from `permissions`)
[PDOException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'portal-for-fun.permissions' doesn't exist
All migrations can be found here: https://github.com/cskiwi/portal-for-fun/tree/master/database
but I think the problem lies here:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRolesTables extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up() {
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('label')->nullable();
$table->integer('role_id')->unsigned()->nullable();
$table->timestamps();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
});
Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('label')->nullable();
$table->timestamps();
});
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')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->primary(['permission_id', 'role_id']);
});
Schema::create('role_user', function (Blueprint $table) {
$table->integer('role_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->primary(['role_id', 'user_id']);
});
Schema::create('permission_user', function (Blueprint $table) {
$table->integer('permission_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
$table->primary(['permission_id', 'user_id']);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down() {
Schema::dropIfExists('permission_user');
Schema::dropIfExists('role_user');
Schema::dropIfExists('permission_role');
Schema::dropIfExists('permissions');
Schema::dropIfExists('roles');
}
}
the database itself only contains an empty migration table
Create permission model in your project.
If you have permission model make sure that name in permission not Permission
I've had similar problem. Try to seaprate migrations and move part of them (those which use permissions table) from migrations folder. First, you should run migration which will create permissions table with php artisan migrate. Then move back migrations which create permission_role and permission_user tables to the migrations folder and run php artisan migrate again.
That helped me, hope it'll help you too.
there. I'm trying to deal with Laravel Framework, but there is a problem. Here is my code
public function up()
{
Schema::create('users', function($table)
{
$table->increments('id');
});
}
I made migration and tried to add some fields to my table. But IDE doesn't recognize $table variable properly, so as a result I have warning: "Method increments doesn't found in class", and I can't use auto-completion.
Are there propositions how to fix this?
Whole code:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration {
public function up()
{
Schema::create('posts', function($table)
{
$table->increments('id');
$table->string('title',150);
$table->text('body');
$table->string('preview',300);
$table->string('author',100);
$table->timestamps();
});
}
public function down()
{
Schema::drop('posts');
}
}
Just specify the type of $table. The class is Illuminate\Database\Schema\Blueprint
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
});