I would like to extend the existing User model in Laravel 5.0 to add new columns to the table. How can I do so ?
Create migration by running command:
php artisan make:migration users_disabled_column
where disabled is name of column you want to add to existed table.
Edit new migration with adding column, here is example:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class UsersDisabledColumn extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function($table) {
$table->boolean('disabled')->default(false);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('users', function($table) {
$table->dropColumn('disabled');
});
}
}
Execute created migration:
php artisan migrate
Now you can use new column:
$user = User::find($id);
$user->disabled = false;
$user->save();
Related
I need to change my migration files because currently it's using model but I've read that we should not be using models inside migration files.
I have an eloquent code:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use App\MyModel;
class AddRelationshipIdToMyModel extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
MyModel::query()->forceDelete();
Schema::table('my_model', function (Blueprint $table) {
$table->foreignId('my_model_id')->constrained();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('my_model', function (Blueprint $table) {
$table->dropForeign(['my_model_id']);
});
}
}
I need to refactor this to not use model 'MyModel' but I don't know the equivalent for this in db query
I understand that you want to Truncate the table before running the migration.
If I'm right then you need to use DB::table() and truncate() function.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use App\MyModel;
class AddRelationshipIdToMyModel extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
\DB::table('my_model')->truncate();
Schema::table('my_model', function (Blueprint $table) {
$table->foreignId('my_model_id')->constrained();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('my_model', function (Blueprint $table) {
$table->dropForeign(['my_model_id']);
});
}
}
Can anyone know how to create Table in laravel without created_at and Updated_at Column in table using a migration.
I try this Following code but it's still create created_at and Updated_at column in table:-
Migration Laravel :-
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateNewusersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('newusers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->text('name');
$table->string('email', 100)->unique();
$table->bigInteger('phone_no');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('newusers');
}
}
Add this in your model
public $timestamps = false;
I want to create a table by migration with name "blogOST".
But Laravel create a table is "blogpost".
In Laravel, how to use migration to create table name with camelCase format.
This is my migration code
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOrderDetailTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('blogPost', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title', 100);
$table->dateTime('createdAt');
$table->dateTime('updatedAt');
$table->dateTime('deletedAt')->nullable();
$table->charset = 'utf8';
$table->collation = 'utf8_general_ci';
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('orderdetail');
}
}
Mostly its a common way to usesnake casefor table names and column names, Not onlyLaravel` but also other languages uses same practice.
In Laravel Eloquent Model automatically maps to lower case plural table name. You can explicitly mention different table name if you want using protected $table = 'my_table_name';
So its Laravels default behavior.
I have read several threads about this but still not exactly my problem.
So in the users table I have a column called role, which is enum type and has two available values: 1 and 2. I set 2 as the default one. Now I want to change it to 1, for example. I created a new migration, ran php artisan migrate and encounter this error:
[Illuminate\DatabaseQueryException]
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'role' (SQL: alter table `u
sers` add `role` enum('1', '2') not null default '1')
[PDOException]
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'role'
Here is code in my CreateUsersTable migration file:
$table->enum('role', ['1', '2'])->default('2');
And I did the same in the new UpdateUsersTable migration file:
$table->enum('role', ['1', '2'])->default('1');
And by the way I can not use php artisan migrate:refresh because it will delete all my data. Where am I doing wrong?
$table->enum('role', ['1', '2'])->default('1')->change();
<?php
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Migrations\Migration;
class UpdateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
DB::statement('ALTER TABLE `users` MODIFY `role` DEFAULT 1;');
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
DB::statement('ALTER TABLE `users` MODIFY `role` DEFAULT 2;');
}
}
Adapted from this answer to another question
In order to do this you have to add ->nullable() or ->default() to every field you add to the migration file.
create a new migration file
php artisan make:migration add_role_to_users_table --table=users
open the created migration file (database\migrations\2021_12_01_050851-add_role_tables.php) and add below code.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddRoleToUsers extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->enum('role', ['1', '2'])->default('1')->comment('1 - admin, 2 - normal'); //added
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('role'); //added
});
}
}
?>
Now migration refresh
php artisan migrate:refresh --path=/database/migrations/2021_12_01_050851-add_role_tables.php
Is there any way/laravel-command to drop a specific table from the production server?
Set up a migration.
Run this command to set up a migration:
php artisan make:migration drop_my_table
Then you can structure your migration like this:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class DropMyTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
// drop the table
Schema::dropIfExists('my_table');
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
// create the table
Schema::create('my_table', function (Blueprint $table) {
$table->increments('id');
// .. other columns
$table->timestamps();
});
}
}
You can of course just drop and not check for existence:
Schema::drop('my_table');
Read further in the docs here:
https://laravel.com/docs/5.2/migrations#writing-migrations
You may also have to consider dropping any existing foreign keys/indexes, for example if you wanted to drop a primary key:
public function up()
{
Schema::table('my_table', function ($table) {
$table->dropPrimary('my_table_id_primary');
});
Schema::dropIfExists('my_table');
}
More in the docs in dropping indexes etc here:
https://laravel.com/docs/5.2/migrations#dropping-indexes