Method increments doesn't found in class. Laravel - php

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');
});

Related

Why do I need to call Schema::table in Laravel more than once?

I'm on a Udemy course about Laravel 7. In a particular class, I need to rename the column of a table called site_contacts. When I try to simplify the code and I and put two instructions in a callback the error occurs: There is no column with name 'contact_reason_id' on table 'site_contacts'. This occurs when you reach the statement to change the column type. But, if I write separately, as it is in the code, migration runs without problems.
I would like to understand why it needs to be done this way and if there's any better way. Initially, I think the class needs to start with its updated internal attributes to proceed with the statements, otherwise it doesn't know that the column is under another name.
This code does not work.
public function up()
{
Schema::table('site_contacts', function (Blueprint $table) {
// There is no column with name 'contact_reason_id' on table 'site_contacts'.
$table->renameColumn('contact_reason', 'contact_reason_id');
$table->unsignedBigInteger('contact_reason_id')->change();
});
Schema::table('site_contacts', function (Blueprint $table) {
$table->foreign('contact_reason_id')->references('id')->on('contact_reasons');
});
}
This code works.
public function up()
{
Schema::table('site_contacts', function (Blueprint $table) {
$table->renameColumn('contact_reason', 'contact_reason_id');
});
Schema::table('site_contacts', function (Blueprint $table) {
$table->unsignedBigInteger('contact_reason_id')->change();
});
Schema::table('site_contacts', function (Blueprint $table) {
$table->foreign('contact_reason_id')->references('id')->on('contact_reasons');
});
}
public function down()
{
Schema::table('site_contacts', function (Blueprint $table) {
$table->dropForeign('site_contacts_contact_reason_id_foreign');
$table->dropIndex('site_contacts_contact_reason_id_foreign');
});
Schema::table('site_contacts', function (Blueprint $table) {
$table->integer('contact_reason_id')->change();
});
Schema::table('site_contacts', function (Blueprint $table) {
$table->renameColumn('contact_reason_id', 'contact_reason');
});
}
Thanks.

Laravel 5.6: Create database schema using migration

I want to create several schemas to my database using migration. I have this code:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSchemaAdministracion extends Migration
{
public function up()
{
DB::unprepared("CREATE SCHEMA `administracion`");
}
public function down()
{
DB::unprepared('DROP SCHEMA `administracion`');
}
}
I tried this way but I got: Invalid schema name: 7 ERROR: No schema has been selected.
Try using DB::statement with DB::raw.
I tested this with Laravel 5.8. I guess it should work with earlier versions too.
\DB::statement(\DB::raw("CREATE SCHEMA administracion"));
You can then use it with the schema as prefix. Like this:
Schema::create('administracion.mytable', function (Blueprint $table) {
$table->bigIncrements('id');
...
});
Notice for rollback operation: consider that in postgres you cannot drop a schema if it has elements. You should remove them before removing the schema.
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAdministracionTable extends Migration
{
public function up()
{
Schema::create('administracion', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('airline');
$table->timestamps();
});
}
public function down()
{
Schema::drop('administracion');
}
}

ErrorException Undefined index: 2017_05_14_071616_create_users_table

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');
}
}

How to change data type and add column with migration without losing data? (laravel 5.3)

My migration is like this :
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->increments('id_test');
$table->string('name', 50);
$table->timestamps();
$table->softDeletes();
});
}
public function down()
{
Schema::drop('tests');
}
I want to change data type and add column in table test. Then update table in database. I edit like this :
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->increments('id');
$table->string('id_test', 18);
$table->string('name', 50);
$table->timestamps();
$table->softDeletes();
});
}
public function down()
{
Schema::drop('tests');
}
Then I run : php artisan migrate:refresh
I lost my data in database
Is there any people who can help me?
Create a new migration file using below command :
php artisan make:migration name_of_your_file
And its function should be like this
public function up()
{
Schema::table('tests', function($table) {
$table->renameColumn('id_test', 'id');
$table->string('id_test', 18);
});
}
public function down()
{
Schema::table('tests', function($table) {
$table->dropColumn('id_test');
});
}
Now just run below command
php artisan migrate
Note : Dont use refresh it deletes the data
To know more about Laravel Migration refer : https://laravel.com/docs/5.3/migrations#modifying-columns

Laravel migration inheritance is not working

I'm new to Laravel and php so I face and error I don't know how to solve.
The basic problem is since a lot of tables have primary:id and created_by,updated_by columns what I've figured out is inheriting them in my migrations.
I'm using php7
So I have a Base class
class BaseMigration extends Migration {
public function up(string $tableName) {
Schema::create($tableName, function (Blueprint $table) {
$table->mediumIncrements('id');
$table->primary('id');
$table->unsignedMediumInteger('created_by')->references('id')->on('users');
$table->unsignedMediumInteger('updated_by')->references('id')->on('users');
});
}
}
and the extending migration
class CreateItemsTable extends BaseMigration {
public function up() {
parent::up('items');
Schema::create('items', function (Blueprint $table) {
$table->string('name', 74);
$table->timestamps();
});
}
// ......
}
However php artisan migrate gives me this:
[ErrorException] Declaration of CreateItemsTable::up() should be compatible with Illuminate\Database\Migrations\BaseMigration::up(string $tableName)
Is it because I'm running double up() ?
What am I missing? Appreciate your kind help.
I think you need to have the same function signature, so pass string $tableName:
class CreateItemsTable extends BaseMigration {
public function up(string $tableName) {
Schema::create('items', function (Blueprint $table) {
parent::up('items');
$table->string('name', 74);
$table->timestamps();
});
}
// ......
}

Categories