This is users migration code:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('nickname')->unique();
$table->string('email')->unique();
$table->string('password', 60);
$table->string('pic_url')->nullable();
$table->string('language')->default('en');
$table->string('mobile_phone')->nullable();
$table->string('work_phone')->nullable();
$table->string('website')->nullable();
$table->string('twitter')->nullable();
$table->string('facebook')->nullable();
$table->string('description')->nullable();
$table->string('time_zone')->nullable();
$table->integer('rate_val')->nullable();
$table->integer('rate_count')->nullable();
$table->enum('role', array_keys(trans('globals.roles')))->default('person');
$table->enum('type', array_keys(trans('globals.type_user')))->default('normal');
$table->enum('verified', array_keys(trans('globals.verification')))->default('no');
$table->json('preferences')->nullable();
$table->rememberToken();
$table->timestamps();
$table->timestamp('disabled_at')->nullable();
$table->softDeletes();
});
}
"php artisan migrate:refresh" When I am trying to run this command I am getting error which is showing in image.
Please help me to resolve this issue.
Thanks in advance.
It seems there is known issue in laravel in some mysql versions please check here
For now if you want to resolve this issue use 'text' instead of json
Related
I have some problem with the Laravel 9 migrations. After i run the "php artisan migrate" command every table created with the indexes. So everthings just fine except the foregin key. I don't have any idea why, maybe someone know what causes the problem or have a solotion. Thank you all!
10.4.22-MariaDB
InnoDB
Apache/2.4.52 (Win64) OpenSSL/1.1.1m PHP/8.1.2
phpMyAdmin: 5.11.1
My migrations:
Schema::create('supplier_orders', function (Blueprint $table) {
$table->increments('Id');
$table->unsignedBigInteger('Warehouse', false)->nullable(false);
$table->unsignedBigInteger('Supplier', false)->nullable(false);
$table->dateTime('StartedAt')->nullable(false)->useCurrent();
$table->dateTime('CompletedAt')->default(null);
$table->string('PrimeVoucherNumber', 100)->nullable(false);
$table->index(['CompletedAt', 'Warehouse', 'Supplier'], 'CompletedWarehouseSupplier');
});
Schema::create('supplier_order_details', function (Blueprint $table) {
$table->increments('Id');
$table->unsignedInteger('SupplierOrder', false)->nullable(false);
$table->unsignedBigInteger('Employee', false)->nullable(false);
$table->unsignedBigInteger('Product', false)->nullable(false);
$table->unsignedDecimal('Quantity', 18, 4)->nullable(false);
$table->index(['SupplierOrder', 'Employee', 'Product'], 'OrderEmployeeProduct')->unique();
$table->index(['Product', 'SupplierOrder', 'Quantity'], 'ProductOrder');
});
Schema::table('supplier_order_details', function (Blueprint $table) {
$table->foreign('SupplierOrder', 'FK_SupplierOrderDetail_SupplierOrder')->references('Id')->on('supplier_orders')->onDelete('CASCADE')->onUpdate('NO ACTION');
});
I found it, there is no error in the migartions, just me.
I am just a big donkey.
I found it in the phpmyadmin under "Table->Structure->Realation view"
I thought that i will found it under "Table->Structure->Table structure->Indexes"
Its make no sense to me why is it there, but finally i found it.
Thanks everyone.
Friends I have the following problem with laravel migrations using postgres, and when I make changes to a migration, in this case the users table, but I get an error trying to remove an index from a key, can you help me please with this problem.
This is my migration code:
public function up() {
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->integer('idProfile');
$table->string('name');
$table->string('surname');
$table->string('email')->unique();
$table->string('photo')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
public function down() {
Schema::dropIfExists('users');
Schema::table('users', function (Blueprint $table){
$table->dropPrimary('id');
$table->dropIndex('users_pkey');
});
}
response from my console:
These are the indices that list me:
This is the structure of the final table:
Comments, things to improve I am all ears
When you are running migrate:refresh, you are running the down method. In your down method, you are dropping the table, and then trying to make edits to it, which is why you are getting "users" doesn't exist.
If this is just in a development env, make your changes in the up method, and remove everything apart from dropIFExists() from the down method.
It is highly recommended that don't change the migration file...
If you need to add a new row to your table (Consider you have mytable and you want to add myrow to the table), you can write in terminal :
php artisan make:migration add_myrow_to_mytable_table
after that , edit new added migration file!
remember to add the following code in down function :
Schema::table('mytable', function (Blueprint $table) {
$table->dropColumn('myrow');
});
after all, run :
php artisan migrate
If you want to remove a column from your table just follow this one :
Laravel Migrations - Dropping columns
Good Day
I have a Laravel 7 project that's having some issues, first i have this error
The solution that i found for that error is to edit 'strict' value to false in config/database.php. This fixed the error. But now i just realized that every time I create a user the value of the id is always "0" example
Here is my migration
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->string('phone_number');
$table->string('client_address');
$table->rememberToken();
$table->timestamps();
});
}
it is because of your id field in table is not AuthoIncrement
first of all remove users table in database
then remove create_users_table record in migrations table in database
and then change your migration to this
$table->bigIncrements('id');
and then php artisan migrate
I install la-ravel but in only one module admin but i want to different module like as admin,super admin
php artisan make:auth
https://medium.com/justlaravel/how-to-use-middleware-for-content-restriction-based-on-user-role-in-laravel-2d0d8f8e94c6
public function up()
{
Schema::create(‘users’, function (Blueprint $table) {
$table->increments(‘id’);
$table->string(‘name’);
$table->string(‘email’)->unique();
$table->string(‘password’);
$table->strint('adminrole');
$table->string(‘type’);
$table->rememberToken();
$table->timestamps();
});
}
I am writing a code to be scheduled to run occasionally. I want to remove records of users who have their details on the users table but does not have their records in the profiles table as well. I am thinking about removing the by their id, but I am not sure how I can make the match. profiles migration:
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('gender',5);
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); // When a profile is deleted- his corresponding associated id details are deleted as well.
$table->tinyInteger('age')->unsigned()->nullable();
$table->string('goals')->nullable();;
$table->string('activityType')->nullable();
$table->rememberToken(); // for remember me feature.
});
}
Users migration:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('username',20)->unique();
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
I have started to write the query, yet I am not sure how to proceed.
public function handle()
{
// type the query to get users that have no data in their profiles.
$empty_profile=App\Profile:::where('id','')->get;
$user_id=App\User::where('id')
}
Appreciate your help. Thanks.
Although your example is not exactly clear to me (and I wonder why there would be users without profiles in the first place?) I think this is what you need:
public function handle()
{
$empty_profiles = App\User::doesntHave('profile')->delete();
}
This will work if the models are correctly connected see also:
https://laravel.com/docs/master/eloquent-relationships