Laravel QueryException when adding unique constraint - php

I want to create a "users" table with the email record as a unique value with the Migrations file of my Laravel project
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('company')->nullable();
$table->string('street', 80)->nullable();
$table->string('zip', 10)->nullable();
$table->string('city', 80)->nullable();
$table->string('country', 3)->nullable();
$table->string('firstname', 80)->nullable();
$table->string('lastname', 80)->nullable();
$table->string('email', 80);
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->timestamps();
});
works fine, but if I replace $table->string('email', 80); with $table->string('email', 80)->unique(); I get the error SQLSTATE[HY000] [2002] Connection refused (SQL: alter table usersadd uniqueusers_email_unique(email)) I read, that adding Schema::defaultStringLength(191); in AppServiceProvider.php would solve this, but even after adding it and clearing cache with php artisan config:clear && php artisan cache:clear && php artisan view:clear && php artisan route:clear the error is still there.

After trying a bit more I realised, that the error was with my mariadb version, as pointed out in this Comment MariaDB 10.4 supports a UNIQUE KEY on a TEXT column, so I changed $table->string('email')->unique() to $table->text('email')->unique() and it worked.

Related

"php artisan migrate" shows "nothing to migrate"

I am new to laravel.
I am working on laravel version 6.
I have created migration.
It works nicely the first time, but if i change something in the migration file and then i run php artisan migrate it shows nothing to migrate.
I tried php artisan migrate --path as well but it does not work.
To make it work i have to delete the migration file and create it again.
I don't want to use php artisan migrate:fresh.
what should i do to run only one migrations file which has been changed?
When you run php artisan migrate it'll check migration table if there is no new file in the migration folder it'll show you nothing to migrate.
If you want to rollback last migration.
To rollback the latest migration operation, you may use the rollback command. This command rolls back the last "batch" of migrations, which may include multiple migration files:
php artisan migrate:rollback
It'll delete the last created table.
The migrate:reset command will roll back all of your application's migrations:
php artisan migrate:reset
The migrate:fresh command will drop all tables.
php artisan migrate:fresh
php artisan migrate:fresh --seed
more info : document
Sadly impossible. The best workaround is to use seeders and use php artisan db:seed after you use php artisan migrate:fresh. Why don't you want to use that?
there is two things to do you that you can use
1. In your database there is a table called migrations. Delete the rows from there which one is you want to migrate. That should be there.
2.create a folder inside of database/migrations/folder. And put the migrations file inside the folder then in your command prompt run this below command:
php artisan migrate:refresh --path=database/migrations/folder
option 2 is better than the option 1. I always use this. So i recommend option 2. This should be work
If you have seeder data then you simply do: php artisan migrate: fresh -- seed . It help to re-migrate your migration with seeder data
You didn't understand well migrations mechanics.
but if i change something in the migration file and then i run php artisan migrate it shows nothing to migrate
You write migration to make some changes in database and run it once. If you want to do next updates to database you need next migration.
During development process you can rerun migrations by php artisan migrate:fresh, but on production make sure your migration makes everything you want.
Laravel stores informations about migrations in database in table 'migrations'. If you want to reset some migrations files you can try deleting or edit some records in that table.
Have you checked that the migration has already run or not in the migration table.
If its run then there will be a row respective to your migration.
If you do changes to an old migration than nothing will be reflected when you run command php artisan migrate, as it has already been migrated.
For every modification on existing (already migrated tables) you will have to make a new migration with modifications only. If you have "users" table migration 2014_10_12_000000_create_users_table like:
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
and you need to split "name" column, will have to php artisan make:migration alter_table_users --table="users" and add what you want to change:
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('name', 'first_name'); // rename column
$table->string('last_name'); // add new column });
Reverse
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('first_name', 'name');
$table->dropColumn('last_name');
});
}
Nou you can use php artisan migrate
Documentation: https://laravel.com/docs/6.x/migrations#modifying-columns
No one replied question "Akshay Rathod" but "Militaru" replied exactly what he need php artisan make:migration alter_table_users --table="users" and copy you new fields inside the up() function as under
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('userimage')->nullable();
$table->string('contact_no')->nullable();
$table->string('website')->nullable();
$table->string('country_id')->nullable();
$table->string('timezone')->nullable();
$table->string('currency')->nullable();
$table->string('communication_email')->default(1);
$table->string('communication_phone')->default(0);
$table->string('allow_marketing')->default(0);
});
}

How to migrate laravel migration file to mongodb

I fairly new to laravel and a beginner in monogo db. I have been trying to connect mongodb cluster of mongodb atlas in my laravel project.
But when I am trying to migrate the laravel migration file it is showing error saying mysql error even after I changed the default connection to mongodb.
Can anyone please tell me how can I fix this issue and migrate the current project to mongodb?
PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=homestead", "homestead", "secret", [])
1 PDOException::("SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
")
C:\Users\admin\Desktop\test\test\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php : 68
C:\Users\admin\Desktop\test\test\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php : 68
Since laravel doesnt allow mongodb out of the box, so I am using a mongodb package https://github.com/jenssegers/laravel-mongodb
And I also would like to mention that I have monngodb installed in my php as per the documentation. I can see the confirmation of mongodb on phpinfo() page. My settings are as follows:
My .env
DB_CONNECTION="mongodb"
DB_MONGO_PORT=27017
DB_MONGO_DATABASE=test
DB_MONGO_DSN="mongodb+srv://<USERNAME>:<PASSWORD>#cluster0-
***.mongodb.net/test"
My config/database.php
'default' => env('DB_CONNECTION', 'mongodb'),
'mongodb' => [
'driver' => 'mongodb',
'dsn' => env('DB_MONGO_DSN'),
'database' => env('DB_MONGO_DATABASE'),
],
My user migration file
use Illuminate\Support\Facades\Schema;
//use Illuminate\Database\Schema\Blueprint;
use Jenssegers\Mongodb\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
protected $connection = 'mongodb';
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
}
I'm not sure why it's not working with the default connection set to mongodb, but I've ran into this problem before. The issue I was having is that the connection property in the migration is useless. I had to do the following;
Schema::connection($this->connection)->create('collections', function (Blueprint $table) {
$table->increments('id');
$table->index('slug');
$table->index('world_id');
$table->unique(['world_id', 'slug']);
$table->timestamps();
});
Schema::connection('mongodb') should work for you.
put DB_CONNECTION=mongodb on your .ENV file.
Also remove quotes from MOGN0_DSN and make sure to call php artisan config:cache to dump the autoloaded configuration file
Try this package.
It's easy to use.
https://packagist.org/packages/jenssegers/mongodb
Try .env file as
DB_CONNECTION=mongodb
without qoutes

Laravel 5.4: Error with migrations and foreign keys

Having used the appropriate artisan commands:
php artisan make:model VenuesGeoNations --migration
... and:
php artisan make:model VenuesGeoRegions --migration
I created the schema:
Schema::create('venues_geo_nations', function (Blueprint $table) {
$table->increments('id');
$table->string('nation');
$table->string('iso');
$table->timestamps();
});
... and:
Schema::create('venues_geo_regions', function (Blueprint $table) {
$table->increments('id');
$table->string('towns_cities');
$table->string('regions');
$table->string('nations');
$table->mediumInteger('venues_geo_nation_id')->unsigned();
$table->foreign('venues_geo_nation_id')->references('id')->on('venues_geo_nations');
$table->timestamps();
});
However, when I run the migration command, I get the error:
[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error:
1215 Cannot add foreign key constraint...
... on the venues_geo_regions table, in spite of the correct execution order.
I've also tried executing in the reverse order and I get the same error.
Compounding things is the fact that each rollback wipes out the tables for Voyager, and if it wasn't for snapshots on the EC2 instance I'm using, I'd have gone mad before now.
At this stage I'm a bit lost, and have to assume I'm doing something wrong.

Laravel default user table

I use Laravel 5.2 and want to change default user table's columns. I wrote
in the CreateUsersTable
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Schema::table('users', function ($table) {
$table->string('role');
$table->string('info');
$table->string('age');
$table->string('hobies');
$table->string('job');
});
}
and i run these commands in the git bash but user table didn't change.
php artisan migrate:refresh
php artisan migrate
How can i solve it?
You should run composer dumpauto -o after running php artisan migrate command to register new migrations, so they could be rolled back.
Try it. If it will not work, try to delete all tables, including migrations and run this command:
php artisan migrate:install
create new migration file and do something like this
public function up() {
DB::statement('ALTER TABLE user ADD some_field INT');// your query here
}
php artisan migrate
You only need to change what it inside of Schema::create and delete your Schema::table
Then, you need to implement tour down() mehod and add Schema::drop('users'); in order to drop your table when you run migrate:refresh.
You can read the 'Database migrations' in the laravel documentation for further details.
delete the first statement and leave last one only .
then run php artisan migrate:refresh

Laravel migration error, pluck() undefined

I have a fairly simple migration that produces the following error:
[Symfony\Component\Debug\Exception\FatalErrorException]
Call to undefined method Illuminate\Support\Collection::pluck()
I can rollback but I cannot migrate up. I'm using "laravel/framework": "5.1.*#dev", and this is the migration:
public function up()
{
Schema::table('tutorials', function (Blueprint $table) {
$table->string('url');
$table->string('title');
$table->text('description');
$table->string('image');
});
}
The pluck method is defined in Illuminate\Support\Collection so I'm sort of at a loss. I've run composer dump, as well. Any ideas?
Consider using laravel version "5.1.*" which will evaluate to 5.1.4
This is because #dev is constantly under development and not all of its functional may be in place. Today one thing will work while the next day another fails. Recommendation is to use a stable version then run the migrations to see if they work.

Categories