So I have a lot of migration files from a previous database setup. I'd like to apply these files to a new database. For example, if I were starting from scratch I would do something like this:
php artisan make:migration create_users_table
and then:
php artisan migrate
for every single migration file. But since I already have all the migration files, is there a way to skip all of the make:migration part and get it to create the corresponding tables?
I've tried:
php artisan migrate --force
but it doesn't work. What's the right way to do this? This is a clean installation of Laravel 5.5.
I'm getting this error:
This is the schema where the error is happening:
use Illuminate\Support\Facades\Schema;
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('id');
$table->string('username');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamp('created_at');
$table->timestamp('updated_at');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
I am unable to post comments so posting this as an answer.
try $table->timestamps();
Don't write created_at and updated_at separately.
Also try this:-
$table->string('remember_token', 100) -> nullable();
This seems to be the last hope now.
try $table->timestamps() -> nullable();
Set strict to true in my MySQL config (in .env or config/database.php).
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true
]
],
Related
I'm adding these values to an existing table. In my down() function, how do I remove only the items I just added? truncate() will remove all the items in the table. I can use DB::table('user_account_cancel_reasons')->where('action', '==', 'cancel')->delete();, but that doesn't guarantee only removing the items in this migration. Using Laravel 7.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddNewFieldsToUserAccountChangeReasons extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
DB::table('user_account_change_reasons')->insert(
[
[
'reason' => 'Health Issues',
'action' => 'cancel',
'created_at' => DB::raw('CURRENT_TIMESTAMP'),
'updated_at' => DB::raw('CURRENT_TIMESTAMP')
],
[
'reason' => 'Did not use',
'action' => 'cancel',
'created_at' => DB::raw('CURRENT_TIMESTAMP'),
'updated_at' => DB::raw('CURRENT_TIMESTAMP')
],
[
'reason' => 'Other',
'action' => 'cancel',
'created_at' => DB::raw('CURRENT_TIMESTAMP'),
'updated_at' => DB::raw('CURRENT_TIMESTAMP')
],
]
);
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('user_account_change_reasons', function(Blueprint $table) {
DB::table('user_account_change_reasons')->truncate();
});
}
}
Try, i solved it
public function down()
{
Schema::table('user_account_change_reasons', function(Blueprint $table)
{
DB::table('user_account_change_reasons')->where('action',
'cancel')->delete();
});
}
public function up()
{
Schema::create('settings', function (Blueprint $table) {
$table->id();
$table->string('name', 40)->unique();
$table->json('value');
$table->timestamps();
});
//seeder to insert FTP settings
DB::table("settings")->insert([
'name' => 'FTP_SETTINGS',
'value' => ['host' => '192.168.5.190', 'username'=> 'Alessandro', 'password' => 'Alessandro', 'port' => '21']
]);
}
I'm doing this migration with a seeder after that (I've also put it into the seeder section but has the same issue) but i get the ErrorException Array to string conversion.
Probably is something with the value propriety but I cannot understand what I'm doing wrong..many thanks for your help.
You are trying to insert array values into json filed.
Try instead:
DB::table("settings")->insert([
'name' => 'FTP_SETTINGS',
'value' => json_encode(['host' => '192.168.5.190', 'username'=> 'Alessandro', 'password' => 'Alessandro', 'port' => '21'])
]);
I have this software and I need to create new institutions (like kindergardens). How to migrate into that new database?
if ($newDb) {
Config::set('database.connections.th', [
'driver' => 'mysql',
'host' => 'localhost',
'port' => '3306',
'database' => 'kindergarden'.$institutionId,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'timezone' => '+00:00',
'strict' => false,
]);
return \Illuminate\Support\Facades\Artisan::call('migrate');
}
And migrations are like
public function up()
{
Schema::create('students', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('first_name', 100);
$table->string('last_name', 100);
$table->tinyInteger('gender');
$table->date('date_of_birth');
$table->string('address', 150);
$table->string('phone_number', 20);
$table->string('email', 100);
$table->unsignedTinyInteger('status')->default(1);
$table->timestamps();
$table->softDeletes();
});
}
Set the non default database connection in your table migration
Schema::connection('th')->create('students', function (Blueprint $table)
This will inform artisan to migrate this table to the specified database in your configuration
From the Docs
Database Connection & Table Options
If you want to perform a schema operation on a database connection that is not your default connection, use the connection method:
Schema::connection('foo')->create('users', function (Blueprint $table) {
$table->bigIncrements('id');
});
My Laravel project uses the following user types: students, parents, trainers. Now I would like to use Laravel Nova for the backend to manage the different resources.
Nova uses the users table, and model as default, however, I would like to use the admins table and model for the login.
I already created a custom admins table and model and updated the config/auth.php.
database/migrations/create_admins_table.php
...
public function up()
{
Schema::create('admins', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name', 60);
$table->string('email', 60)->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
config/auth.php
'guards' => [
...
'admins' => [
'driver' => 'session',
'provider' => 'admins',
],
],
'providers' => [
...
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
],
What changes do I have to make to use the admins' table/guard for the Nova login?
In your /config folder, you'll find the file nova.php. Inside it, change the following line of code to specify your guard. For example:
'guard' => 'admins',
I have a problem with a foreign key.
I have a user who has a sex:
My migration users :
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('nom');
$table->string('prenom');
$table->string('adresse');
$table->integer('cp');
$table->string('ville');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->tinyInteger('admin')->nullable();
});
Schema::table('users', function ($table) {
$table->integer('sexe_id')->unsigned();
$table->foreign('sexe_id')->references('id')->on('sexes');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('users');
}`
My sexe migration :
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('sexes', function (Blueprint $table) {
$table->increments('id');
$table->string('libelle');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('sexes');
}
My sexes seeder :
class SexesTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
DB::table('sexes')->insert([
[
'libelle' => 'Homme',
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
],
[
'libelle' => 'Femme',
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
]
]);
}
}
My users seeder :
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
DB::table('users')->insert([
[
'name' => 'admin',
'nom' => 'Virlois',
'prenom' => 'Peter',
'sexe_id' => 1,
'email' => 'admin#admin.fr',
'adresse' => '12 rue Jean Rostand',
'cp' => 90000,
'ville' => 'Belfort',
'password' => bcrypt('admin123'),
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
'admin' => 1,
],
[
'name' => 'test',
'nom' => 'Mennegain',
'prenom' => 'Mathieu',
'sexe_id' => 1,
'email' => 'test#test.fr',
'adresse' => '12 rue Jean Rostand',
'cp' => 90000,
'ville' => 'Belfort',
'password' => bcrypt('test123'),
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
'admin' => 0,
]
]);
}
}
My database seeder :
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
$this->call(SexesTableSeeder::class);
$this->call(UsersTableSeeder::class);
$this->call(SaisonTableSeeder::class);
$this->call(ProduitTypeSeeder::class);
$this->call(SportsTableSeeder::class);
$this->call(ProduitsTableSeeder::class);
}
}
When i run : php artisan migrate:refresh -seed
I have this error :
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL
: alter table `users` add constraint `users_sexe_id_foreign` foreign key (`
sexe_id`) references `sexes` (`id`))
[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
The order of migrations is super important. From what you posted you run the users migration first and in that same migration you try to create a users table and alter a table. the other table sexes doesn't exist
($table->foreign('sexe_id')->references('id')->on('sexes');)
so that is why you get a error.
I suggest separating the migrations in order to run users, sexes, alter users or sexes, users and alter in the same migration, but this is not a good way to do things, I mean to mix migrations (create and alter).
Probably there are nothing with id 1 in sexes table and sexe_id => 1 generating this error. In your UsersTableSeeder rather than hardcoding 'sexe_id' => 1 query your sexes table for any row and use that dynamic id.