Laravel Migration - table not getting created - mysql

I'm trying to learn Laravel. I'm following the Quickstart documentation but have gotten into a problem with Migrations. I'm at this step: http://laravel.com/docs/quick#creating-a-migration
When I run the command php artisan migrate, the command line displays the following:
c:\wamp\www\laravel>php artisan migrate
Migration table created successfully.
Migrated: 2013_09_21_040037_create_users_table
In the database, I see a migrations table created with 1 record. However, I do not see a users table. So, I can't continue on to the ORM portion of the tutorial.
Any ideas what I might be doing wrong? Why isn't the users table getting created?
EDIT 1 (original migration file):
<?php
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function($table)
{
$table->increments('id');
$table->string('email')->unique();
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('users');
}
}

The updated Laravel should use Blueprint for database schema creation.
So try to change your user migration file content like this,
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function(Blueprint $table) {
$table->integer('id', true);
$table->string('name');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('password');
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('users');
}
}
Then run,
php artisan migrate:rollback
and then migrate again.
Read the API documentation here http://laravel.com/api/class-Illuminate.Database.Schema.Blueprint.html

I faced the same problem, after searching at Laravel documentation (link to documentation), i found an artisan command that resolve it :
php artisan migrate:refresh
The migrate:refresh command will roll back all of your migrations and then execute the migrate command. This command effectively re-creates your entire database

Related

Can't change data type in database using Laravel migration

I'm trying to change the data type (from string to boolean) of 5 columns in my database, but it's showing an error that i have no idea what is the meaning.
My migration is this:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class ChangeFieldDataType extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->boolean('categoria_pneu')->change();
$table->boolean('categoria_dicas')->change();
$table->boolean('categoria_servicos')->change();
$table->boolean('categoria_dicas_gerais')->change();
$table->boolean('categoria_variados')->change();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('categoria_pneu');
$table->dropColumn('categoria_dicas');
$table->dropColumn('categoria_servicos');
$table->dropColumn('categoria_dicas_gerais');
$table->dropColumn('categoria_variados');
});
}
}
The erros that's showing, is this:
I don't know what is the meaning of the error, since i'm trying to change the type to BOOLEAN and not TINYINT (i don't actually know if it's the same...)

I would like to add a column to existing table on Laravel

$ mysql --version
mysql Ver 8.0.19 for osx10.14 on x86_64 (Homebrew)
I made migration file.
$ php artisan make:migration add_request_to_users_table --table=users
Created Migration: 2020_07_05_231205_add_request_to_users_table
I modified the file migration made like this
class AddRequestToUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('request');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
//
});
}
}
$ php artisan migrate
Migrating: 2020_07_05_231205_add_request_to_users_table
Migrated: 2020_07_05_231205_add_request_to_users_table (0.01 seconds)
But the "request" column was not added to users tables. How come?
There is nothing wrong with your code.
if you use the following import :
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
It should work.
Try to refresh and check database or db schema again.

Laravel Migration taking too long

I'm facing a weird experience with Laravel Migration.
I've run 2 migrations the first one passed in less than second.
The second is taking a long time and still running for more than 10 minutes untill I killed it.
It's not a big deal, just adding 2 columns to a table that contains 23 entries. Yes, only 23 entries.
the query itslef is very simple :
Schema::table('customers', function (Blueprint $table) {
$table->boolean('public')->default(0);
$table->integer('user_id')->nullable();
});
Any clue ? I'm missing anything ?
Is it because I already have 30 columns in the table ?
==== Edit ====
As asked, here are the other migration that passed
class UpdateUsers extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->integer('shop_id')->nullable();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
the one that is not working
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('customers', function (Blueprint $table) {
$table->boolean('public')->default(0);
$table->integer('user_id')->nullable();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
}
}
Finally, I understand what's going on.
I had some slowness when performing some requests. So I thought about the MySQL serve.
I restarted it and then launched the same migration that used not to work and it went perfect (0.07 seconds).
I think checked and / or restarting the BD server is something to check systematically as well as checking the syntax and table structure.
Cheers

How to change database column 'null' to 'nullable' using laravel migration in mysql?

This is my vehicles table. I want to change my database structure by using a migration
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateVehiclesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('vehicles', function (Blueprint $table) {
$table->increments('id');
$table->string('image')->nullable();
$table->string('reg_no')->unique();
$table->string('fuel_type');
$table->string('capacity');
$table->double('rate');
$table->boolean('req_carrier');
$table->date('service_date');
$table->integer('service_freq_km');
$table->integer('service_freq_months');
$table->date('insurance_date');
$table->integer('insurance_freq_months');
$table->date('eco_date');
$table->integer('eco_freq_months');
$table->date('licence_date');
$table->integer('licence_freq_months');
$table->integer('current_company');
$table->string('status')->default("available");
$table->timestampsTz();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('vehicles');
}
}
I want to give nullable values to these columns.
1.service_date
2.service_freq_km
3.service_freq_months
How can I alter these columns as nullable in mysql?
You can read the docs about Modifying Columns.
If you want these feature, you need to install this package first:
composer require doctrine/dbal
Then, you need to create another migration, for example:
2019_10_24_xxxxxx_change_columns_to_nullable_in_vehicles.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class ChangeColumnsToNullableInVehicles extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('vehicles', function (Blueprint $table) {
$table->date('service_date')->nullable()->change();
$table->integer('service_freq_km')->nullable()->change();
$table->integer('service_freq_months')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('vehicles', function (Blueprint $table) {
$table->date('service_date')->nullable(false)->change();
$table->integer('service_freq_km')->nullable(false)->change();
$table->integer('service_freq_months')->nullable(false)->change();
});
}
}
install the package in order to update the tables composer require doctrine/dbal
Since you have migrated the migration files, you now need to create a new migration file using artisan command:
php artisan make:migration change_nullable_field_columns_to_vehicles_tables --table=vehicles
In newly created migration file, add the following codes
public function up() {
Schema::table('vehicles', function (Blueprint $table) {
$table->date('service_date')->nullable()->change();
$table->integer('service_freq_km')->nullable()->change();
$table->integer('service_freq_months')->nullable()->change();
});
}
//For php artisan down
public function down(){
Schema::table('vehicles', function (Blueprint $table) {
$table->date('service_date')->nullable(false)->change();
$table->integer('service_freq_km')->nullable(false)->change();
$table->integer('service_freq_months')->nullable(false)->change();
});
}
Now you can execute migrate command
php artisan migrate
You need to create new migration file that is name "2019_10_24_00000_update_vehicle_tables"
if(Schema::hasTable('vehicles')) {
Schema::table('vehicles', function($table)
{
$table->date('service_date')->nullable();
$table->integer('service_freq_km')->nullable();
$table->integer('service_freq_months')->nullable();
});
}
create new migration class for alter table and use this up function
public function up()
{
Schema::table('vehicles', function (Blueprint $table) {
$table->date('service_date')->nullable();
$table->integer('service_freq_km')->nullable();
$table->integer('service_freq_months')->nullable();
});
}
Schema::table use for alter table and Schema::create is use for create new table
$table->string('first_name')->default('DEFAULT');
If the default value is supposed to be null, make it nullable instead.
$table->string('name')->nullable();
$table->string('name')->nullable()->default('NULL');
$table->string('name')->nullable()->default(NULL);
$table->string('name')->nullable()->default();

Laravel Settings Service Provider Database Migration

I created the settings table via a migration and implemented a SettingsServiceProvider (based on this post Laravel - Set global variable from settings table). Now it says that the base table can't be found. Of course not, because I need to run the migration first.
[Illuminate\Database\QueryException] SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db.settings' doesn't exist (SQL : select value, name from settings)
Any ideas what to do? I course I could create the table manually but this would destruct the ideas of migrations...
Any help would be appreciated. I'm using Laravel 5.3.
Update 25/11/2016 - added code snippets
Service Provider
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class SettingsServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
config()->set('settings', \App\Setting::pluck('value', 'name')->all());
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
//
}
}
Migration
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSettingsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('settings', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('value');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('settings');
}
}