Laravel: Migrations do not work - php

I have created a file Order.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
/**
* The table associated with the model.
* #var string
*/
protected $table = 'order';
}
Then I have created a migration, called 2016_01_01_111111_create_orders_table.php:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateOrdersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('order', function (Blueprint $table) {
$table->increments('id');
$table->string('orderIdent');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
Then I did the migration with
php artisan migrate
Then, I wanted to add some fields, created a new migration file called 2016_01_02_111111_alter_orders.php:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AlterOrders extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
//
Schema::table('order', function(Blueprint $table)
{
$table->string('selecteddate');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
Then again I did
php artisan migrate
But now I get this message in console:
[Illuminate\Database\QueryException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'order' already exists
What do I need to do, to add the new fields to my table?
Thanks in advance!

Adding this as an answer vs a comment to show what needs to be done. Since rollback was done, up needs to be commented out for now.
public function up()
{ /*
Schema::create('order', function (Blueprint $table) {
$table->increments('id');
$table->string('orderIdent');
$table->timestamps();
}); */
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('order');
}

Try this
public function up()
{
//
Schema::table('order', function($table)
{
$table->string('selecteddate');
});
}
remove Blueprint.

Try this :
php artisan migrate:refresh

Related

Laravel Migrate not creating all table

I want to create all of the tables. But Amount table was not created while migrating. is my problem right or wrong? Or why I am fetching this kind of error. here is my all migration code is
**user **
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Year_month
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class YearMonth extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('year_months',function(Blueprint $table){
$table->unsignedBigInteger('id');
$table->foreign('id')->references('id')->on('users')->onDelete('cascade');
$table->date('ym_id')->primary();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('year_months');
}
}
Meal Storage
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class MealStorage extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('meal_storages', function (Blueprint $table) {
$table->unsignedBigInteger('id');
$table->foreign('id')->references('id')->on('users')->onDelete('cascade');
$table->date('ym_id');
$table->foreign('ym_id')->references('ym_id')->on('year_months')->onDelete('cascade');
$table->unsignedBigInteger('member_id');
$table->foreign('member_id')->references('member_id')->on('members')->onDelete('cascade');
$table->date('date');
$table->integer('meal');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('meal_storages');
}
}
Member
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class MealStorage extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('meal_storages', function (Blueprint $table) {
$table->unsignedBigInteger('id');
$table->foreign('id')->references('id')->on('users')->onDelete('cascade');
$table->date('ym_id');
$table->foreign('ym_id')->references('ym_id')->on('year_months')->onDelete('cascade');
$table->unsignedBigInteger('member_id');
$table->foreign('member_id')->references('member_id')->on('members')->onDelete('cascade');
$table->date('date');
$table->integer('meal');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('meal_storages');
}
}
amount
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Amount extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('amounts',function(Blueprint $table){
$table->unsignedBigInteger('id');
$table->foreign('id')->references('id')->on('users')->onDelete('cascade');
$table->unsignedBigInteger('member_id');
$table->foreign('member_id')->references('member_id')->on('members')->onDelete('cascade');
$table->integer('amount');
$table->date('date');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('amounts');
}
}
*is that my process right or wrong *
when I migrate I am fetching this problem
enter image description here
Hey you got this error because you try to run member migration twice
check your migration if you have tow migration for this table try ro delete one or if you using a library check maby the have same migration too

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (Laravel)

I keep getting this error. I understood it's a problem with the foreign key but i really can't understand what it's not working properly. I'm trying to make a many to many relationship with a bridge table. Please, if you can help me solve this! Here you can see the migrations:
Table dresses:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Dress extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('dresses', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('dress_id');
$table->string('name')->unique();
$table->string('brand');
$table->decimal('price');
$table->string('fabric');
$table->string('size');
$table->string('type');
$table->string('wash_instruction');
$table->string('product_details');
$table->string('fit');
$table->rememberToken();;
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
Table Wishlist:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Wishlist extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('wishlists', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('wishlist_id');
$table->foreign('wishlist_id')
->references('user_id')
->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
Bridge table:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateWishlistDressTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('wishlist_dress', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('dress_id');
$table->foreign('dress_id')
->references('id')
->on('dresses')->onDelete('cascade');
$table->unsignedBigInteger('wishlist_id');
$table->foreign('wishlist_id')
->references('id')
->on('wishlists')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('wishlist_dress');
}
}
How is your users migration set up? Do you have a column named users_id in the users migration file?
Usually, only the id column is used, which is generated from $table->id() in the users migrations file. I believe your migrations should work if you change from users_id to id like following:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Wishlist extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('wishlists', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('wishlist_id');
$table->foreign('wishlist_id')
->references('id') //Changed from user_id to id
->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}

Why is Laravel 6.x creating a failed_jobs migration by default

I am creating a messages table in Laravel migrations but it's creating another table too called create_failed_jobs_table. I didn't create this, its a new project. It's happening in every project that I create it automatically creates this table too while creating also the other table, I don't know if its something I've done that creates it. Here is this file:
create_failed_jobs_table):
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateFailedJobsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('failed_jobs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->text('connection');
$table->text('queue');
$table->longText('payload');
$table->longText('exception');
$table->timestamp('failed_at')->useCurrent();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('failed_jobs');
}
}
create_messages_table:)
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateMessagesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('messages', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('messages');
}
}
To answer your question so that your question can be marked as answered: the failed_jobs table comes default with all Laravel 6.x projects. You can check the release note for other things that changed in release 6.0.
Notice that Laravel 6.0 also added a new driver option to the config. That's probably why they've also included the migration by default.

migrate not working after pivot migration of Laravel 5.3

migrate generates the exception after pivot migration of Laravel 5.3.
schema1 : software_houses
schema2 : skill_domains
I generated the command make:migration:pivot skill_domains software_houses
and the migration is created by the name of skill_domain_software_house
but when I execute php artisan migrate
following error occurs :
[Symfony\Component\Debug\Exception\FatalThrowableError]
Class 'CreateSkillDomainSoftwareHousePivotTable' not found
See this Image
SKILL Domain
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSkillDomainsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('skill_domains', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('skill_domains');
}
}
Software House
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSoftwareHousesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('software_houses', function (Blueprint $table) {
$table->increments('id');
$table->string('name',100)->unique();
$table->string('desc',500);
$table->string('url',100);
$table->string('rating')->default('3');
$table->string('logo')->nullable();
$table->string('city')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('software_houses');
}
}
Pivot table created after running the make:migration::pivot command
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSkill_domainSoftware_housePivotTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('skill_domain_software_house', function (Blueprint $table) {
$table->integer('skill_domain_id')->unsigned()->index();
$table->foreign('skill_domain_id')->references('id')->on('skill_domains')->onDelete('cascade');
$table->integer('software_house_id')->unsigned()->index();
$table->foreign('software_house_id')->references('id')->on('software_houses')->onDelete('cascade');
$table->primary(['skill_domain_id', 'software_house_id']);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('skill_domain_software_house');
}
}
Try with below class name line:
class SkillDomainsSoftwareHouses extends Migration
Make sure the file name should be match with class name Documentation.
UPDATE
I think you should try this:
first remove above software_houses,skill_domains from migrations folder also remove from migrations table in your database then follow below steps :
1) php artisan make:migration skilldomain
2) php artisan make:migration softwarehouses
3) php artisan make:migration:pivot skilldomains softwarehouses
4) php artisan migrate

How to create table migration in Laravel 5

I am working on a project where, I have been assigned a task to create user management for the application. But I have stuck on the table relationship and their migration.
Effort
I have these tables:
Users
user_id
username
password
Profiles
profile_id
user_id
firstname
lastname
email
Address
address_id
profile_id
address
city
state
country
pincode
Configurations
config_id
configuration_name
configuration_type
parent_id
Now I have to create model and migration for the same above structure. For this i have create/modify below model and migration class.
Model: User
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');
}
}
Migration: 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');
}
}
Model: Profile
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');
}
}
Migration: 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');
}
}
Model: Addess
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');
}
}
Migration: 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');
}
}
Model: Configuration
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');
}
}
Migration: 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');
}
}
Now, when I run php artisan migrate I am getting error that :
.
Please suggest me how to do that. I have to use same table structure and cannot modify it. If any further update require or I forgot something please let me know.
That happen because migration will try to migrate address table before configuration so it will not found the foreign key config_id you're referenced, so you could change the name of migrations files then the migration commad could pass the configurations_table migrate file first then the addresses_table migrate file, so just change :
2016_02_26_104519_create_configurations_table.php
To :
2016_02_26_102005_create_configurations_table.php
_____________^
After that you should run optimize command to regenerating optimized class loader :
php artisan o
And rerun php artisan migrate command now the problem should be solved.
Hope this helps.

Categories