How to set Auth in laravel and when Two module - laravel-5

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

Related

Undefined table: 7 ERROR: relation "users" does not exist

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

What is the down() function to reverse this Laravel migration that I make?

So, I want to add a column ('Semester_TA') and change the property of this column ('kode_mk') to be unique value, so I created this migration:
public function up()
{
Schema::table('mk', function(Blueprint $table){
$table->string('Semester_TA')->after('sks');
$table->string('kode_mk')->unique();
});
Schema::table('lr2', function(Blueprint $table){
$table->dropForeign(['id']);
});
}
public function down()
{
//
}
I also want to drop/delete this foreign key ('id') that I created as it sometimes give a constraint error in my project. My question is what function down should I write to reverse the update above that i'm about to do in case of migration fails or error?
this is the table "mk" and "lr2" migrations that i use right now:
Schema::create('mk', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('kode_mk');
$table->string('mk');
$table->integer('sks');
$table->integer('semester');
$table->integer('available_seats');
$table->timestamps();
});
Schema::create('lr2', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('k_mk');
$table->string('mk');
$table->string('angkatan');
$table->integer('request_seats');
$table->boolean('status_request');
$table->timestamps();
$table->foreign('id')->references('id')->on('mk');
});
First, you have to create a new migration:
php artisan make:migration Add_column_change_the_property
Then in that migration file up(), try:
public function up()
{
Schema::table('mk', function(Blueprint $table){
$table->string('Semester_TA')->after('sks');
$table->string('kode_mk')->unique();
});
}
Then in the migration file down() try:
public function down()
{
$table->dropColumn('kode_mk');
$table->foreign('id')->references('id')->on('mk');
//
}
Then migrate the file by:php artisan migrate

rename column in laravel using migration [duplicate]

This question already has answers here:
Laravel migrations: Class "not found"
(14 answers)
Closed 3 months ago.
I use laravel 5.6
I want to change author_ID to user_id
I have columns as mentioned below:
class CreatePostsTable extends Migration{
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('author_ID');
$table->text('title');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('posts');
}
}
I use the below link to change my column name :
How can I rename a column in laravel using migration?
then create blow migration :
php artisan make:migration rename_author_id_in_post_table
rename migration file :
class UpdateUsernameInPostTable extends Migration{
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->renameColumn('author_ID', 'user_id');
});
}
public function down()
{
Schema::create('posts', function (Blueprint $table) {
$table->renameColumn('user_id', 'author_ID');
});
}
}
but with the run command : php artisan migrate I have the below error :
Symfony\Component\Debug\Exception\FatalThrowableError :
Class 'RenameAuthorIdInPostTable' not found
Just try this code after deleting your existing migration for updating name,
php artisan make:migration rename_author_id_in_posts_table --table=posts
It will create one migration and then replace up() and down() functions with this in that created migration,
public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->renameColumn('author_ID', 'user_id');
});
}
And down() function with this,
public function down()
{
Schema::table('posts', function (Blueprint $table) {
$table->renameColumn('user_id', 'author_ID');
});
}
For more information about migration, you can go through this link Laravel Migrations.
PROBLEM WITH YOUR MIGRATION:
You are using Schema::create() method for change in table, As you have already created migration for posts table,You don't need to do Schema::create() just use Schema::table() method to update and change in any fields.
Schema::create('posts', function (Blueprint $table) {
$table->renameColumn('author_ID', 'user_id');
});
The error comes from the fact that your class name doesn't correspond to the name of the PHP migration file. Filenames are used to determine the name of the class they contain, so it is important to not rename a class or a file without renaming the other.
Rename your class UpdateUsernameInPostTable to RenameAuthorIdInPostTable and it should work.
If it does not, run composer dumpauto to reload the autoloading file and it will definitely work.
By following the below steps I can be able to rename the column name in Laravel 8
Step 1: Enter the below command to create a new migration file
php artisan make:migration update_author_ID_column --table=posts
Step 2: Write the below code in the created file. Follow the below code for up() function
public function up()
{
Schema::table('employee', function (Blueprint $table) {
$table->renameColumn('author_ID', 'user_id');
});
}
Step 3: Write the Below Code in down() function
public function down()
{
Schema::table('employee', function (Blueprint $table) {
$table->renameColumn('user_id', 'author_ID');
});
}
Step 4: Before running Migration command install the doctrine/dbal dependency
composer require doctrine/dbal
Step 5: finally run migration command.
php artisan migrate
you can use DB statement if you don't want to use doctrine
public function up()
{
DB::statement('ALTER TABLE posts CHANGE author_ID user_id bigint(20) ');
}
Best way would be to check if we have some column in our table or not, just avoid error while migrating;)
public function up()
{
if (Schema::hasColumn('table_name', 'old_col_name'))
{
Schema::table('table_name', function (Blueprint $table)
{
$table->renameColumn('old_col_name', 'new_col_name');
});
}
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
if (Schema::hasColumn('table_name', 'new_col_name'))
{
Schema::table('table_name', function (Blueprint $table)
{
$table->renameColumn('new_col_name', 'old_col_name');
});
}
}

Php artisan migrate:refresh not working

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

Laravel migrate database database using artisan with new columns

I am using laravel and I am trying to do another migration but the table already exists so it is throwing the following error:
[PDOException]
SQLSTATE[42S01]: Base table or view already
exists: 1050 Table 'users' already exists
I've added this line into the migration file:
$table->softDeletes();
This is the full file:
<?php
use Illuminate\Database\Migrations\Migration;
class ConfideSetupUsersTable extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
// Creates the users table
Schema::create('users', function ($table) {
$table->increments('id');
$table->string('firstname');
$table->string('lastname');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('password');
$table->string('confirmation_code');
$table->string('remember_token')->nullable();
$table->softDeletes();
$table->boolean('confirmed')->default(false);
$table->timestamps();
});
// Creates password reminders table
Schema::create('password_reminders', function ($table) {
$table->string('email');
$table->string('token');
$table->timestamp('created_at');
});
}
/**
* Reverse the migrations.
*/
public function down()
{
Schema::drop('password_reminders');
Schema::drop('users');
}
}
Any ideas what I'm doing wrong?
You can rollback the migration and then run it again:
php artisan migrate:rollback
And then:
php artisan migrate
Attention in your case this will delete all data from the table since it will drop users and password reminder and then recreate it.
The alternative is to create a new migration:
php artisan migrate:make users_add_soft_deletes
Put this in there:
Schema::table('users', function ($table) {
$table->softDeletes();
});
And run php artisan migrate to apply the new migration

Resources