Call to undefined method Illuminate\Database\Schema\MySqlBuilder::dropForeign() - php

class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->text('comment');
$table->boolean('approved');
$table->integer('post_id')->unsigned();
$table->timestamps();
});
Schema::table('comments', function (Blueprint $table) {
$table->foreign('post_id')->references('id')->on('posts');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropForeign(['post_id']);
Schema::dropIfExists('comments');
}
}
This is what my migration class looks like, i have been trying to delete the table from the database but it throws me an error.
Error
Call to undefined method Illuminate\Database\Schema\MySqlBuilder::dropForeign()
I have gone through the documentation but it doesnot seem to be of much help.
could anyone please point out my mistake and what would be the solution ?
Just so you know , i am new to laravel.Go easy on me.Thanks!.

dropForeign needs to be called under Schema::table with a Blueprint object,
Schema::table('comments', function (Blueprint $table) {
$table->dropForeign('comments_post_id_foreign');
});
This follows the naming convention of <table_name>_<foreign_table_name>_<column_name>_foreign.
OR
Schema::table('comments', function (Blueprint $table) {
$table->dropForeign(['your_key_name']);
});

You're using wrong syntax. It should be:
Schema::table('comments', function (Blueprint $table) {
$table->dropForeign('comments_post_id_foreign');
});
https://laravel.com/docs/5.3/migrations#foreign-key-constraints

Related

Run specific code on execution of up and down methods of a migration in laravel

I've a Laravel application which has a users table with around 5000 records. The table has a column name which needs to be replaced with first_name and last_name.
With a simple migration script I updated the schema and wrote a simple artisan command to split name into first and last and restored in DB.
Now by rolling back the migration I lose the last_name column along with the data. What I'm looking for is that how can I run something after the execution of up method and before down method.
What would be the most appropriate way to achieve this?
Migration:
class ChangeColumnsInUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('name', 'first_name');
$table->string('last_name')->after('name');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('first_name', 'name');
$table->dropColumn('last_name');
});
}
}
Have you considered added the 'simple script' to the up() and down() functions of the migration itself? Something like this would work right?
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('first_name');
$table->string('last_name');
});
// Fill the first_name and last_name based on the name column
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('name');
});
}
The same can be done for the down() method, except the other way around.

In Macroable.php line 96: Method body does not exist

I'm fairy new to Laravel. I have created a migration file named: create_notes_table and when running command php artisan migrate this error message pops up.
my create_notes_table file content
class CreateNotesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('notes', function (Blueprint $table) {
$table->increments('id');
$table->number('card_id');
$table->body('string');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('notes');
}
}
As the error message states there is no body() method in migrations.
https://laravel.com/docs/8.x/migrations#columns
Change your function up() to :
public function up()
{
Schema::create('notes', function (Blueprint $table) {
$table->increments('id');
$table->integer('card_id');
$table->string('body');
$table->timestamps();
});
}
While we're at it, number() also doesn't exist, changed it to integer()
Edit: changed number to integer

Can't add foreign key Laravel

I have 2 tables (regions & categories) , I wanted to have a foreign key in regions table this is my migration file :
public function up()
{
Schema::create('regions', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('nom');
$table->text('description');
$table->integer('vote');
$table->string('securite');
$table->integer('idCat')->unsigned()->change();
$table->foreign('idCat')->references('idCat')->on('categories');
$table->timestamps();
});
}
the table 'categorie' looks like this :
public function up()
{
Schema::create('categories', function (Blueprint $table) {
//$table->engine = 'InnoDB';
$table->increments('idCat');
$table->String('description');
$table->timestamps();
});
}
But I got this error : Syntax error or access violation: 1072 Key column 'idCat'
doesn't exist in table (SQL: alter table regions add constraint regions_
idcat_foreign foreign key (idCat) references categories (idCat))
Canyou help me please ?
Make sure you get rid of the change(), the column is not there yet.. The following code works perfectly for me...
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class Test extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
//$table->engine = 'InnoDB';
$table->increments('idCat');
$table->String('description');
$table->timestamps();
});
Schema::create('regions', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('nom');
$table->text('description');
$table->integer('vote');
$table->string('securite');
$table->integer('idCat')->unsigned();
$table->foreign('idCat')->references('idCat')->on('categories');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('regions');
Schema::drop('categories');
}
}

Laravel 5.2 Foreign Key fails

I am trying to add a foreign key in my posts table to reference the id in the users table. Here are my migrations, the users table is the default one packed with laravel so the timestamps are from NOV 2014 however the posts is one I created. Any help greatly appreciated.
Thanks a lot :)
Users migration
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')->unsigned();
$table->string('name')->unique();
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('users');
}
}
posts migration
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('title');
$table->string('section');
$table->string('body');
$table->date('post_created_date');
$table->integer('author_id')->unsigned()->after('id');
$table->foreign('author_id')->references('id')->on('users');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('posts');
}
}
Every time I migrate I get the following error:
If you are creating a table, you don't want to use after as it will create a syntax error. Just place the column where you want it. I believe after will only work for alter table statements.
Schema::create('posts', function (Blueprint $table) {
$table->increments('id'); // Unsigned is assumed for increments so I removed that as well
$table->integer('author_id')->unsigned();
$table->string('title');
$table->string('section');
$table->string('body');
$table->date('post_created_date');
$table->timestamps();
$table->foreign('author_id')->references('id')->on('users');
});
Then in your model, you'd setup the relationship to your users table like so...
public function author()
{
return $this->belongsTo(User::class, 'author_id');
}
Feel free to rename the function however you want, I usually prefer to name it something that matches the foreign key.
Then you'd use it like $post->author->name.
In your CreatePostsTable class, can you try this ?
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('author_id')->unsigned();
$table->string('title');
$table->string('section');
$table->string('body');
$table->date('post_created_date');
$table->timestamps();
});
Schema::table('posts', function($table) {
$table->foreign('author_id')->references('id')->on('users');
});
}

Parse Error {} php Migrations

I am getting a parse error on line 50 which is the closing brace for my schema create statement however I cannot see any missing syntax so I am confused.
Code:
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('user_id');
$table->string('f_name');
$table->string('l_name');
$table->string('gender');
$table->date('dob');
$table->string('company_name');
$table->string('email')->unique();
$table->string('password');
$table->increments('landline');
$table->increments('mobile');
$table->increments('activated');
$this->increments('social_login');
$table->timestamp('last_login');
$table->rememberToken();
}
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('users');
}
}
Schema::create(
You never closed that (.
Schema::create('users', function (Blueprint $table) {
$table->increments('user_id');
$table->string('f_name');
$table->string('l_name');
$table->string('gender');
$table->date('dob');
$table->string('company_name');
$table->string('email')->unique();
$table->string('password');
$table->increments('landline');
$table->increments('mobile');
$table->increments('activated');
$this->increments('social_login');
$table->timestamp('last_login');
$table->rememberToken();
});
You are using the increments method incorrectly - Laravel will attempt to make an auto-incrementing primary key for each of the $table->increments(...); statements. Even the fields being specified would not suggest that an auto-incrementing field would be appropriate, you should check out the available methods in the Laravel docs

Categories