Updating table scheme without affecting data in Laravel - php

I am new to Laravel from code igniter and I am LOVING THE FRAMEWORK! My life is so much easier now.
I have created a table with columns using php artisan and entered some test data. I now want to add a few new columns to the database without affecting the current data, and setting the new fields to be null.
My inital thought was to enter a new field in the database migrate file and the run "php artisan migrate", but this just gave me the message "nothing to migrate" and did enter the new column in my database.
Here is my database migrate file:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateFestivalsTable extends Migration {
public function up()
{
Schema::create('festivals', function(Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('title');
$table->timestamps();
});
}
public function down()
{
Schema::drop('festivals');
}
}

create new migration with artisan name it addColumnFestivalTable
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class addColumnFestivalTable extends Migration {
public function up()
{
Schema::table('festivals', function($table)
{
$table->string('new_col_name');
});
}
public function down()
{
Schema::table('festivals', function($table)
{
$table->dropColumn('new_col_name');
});
}
}
for more information read Laravel 5.4 doc

Related

"laravel Migration Foreign key constraint is incorrectly formed"

I have a problem In Laravel and I do not know where the problem is. someone can help me . And thank you in advance.
I have two tables (clients, and reparations)
I want to make a foreign key with the string field ('cin_client'), when I create the table reparations it does not work anymore
/* ************* table clients **************>
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateClientsTable extends Migration
{
public function up()
{
Schema::create('clients', function (Blueprint $table) {
$table->Increments('id_client');
$table->string('nom_client');
$table->string('prenom_client');
$table->string('adresse_client');
$table->string('tel_client');
$table->string('cin_client',30);
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('clients');
}
}
/************* Table reparations *************/
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateReparationsTable extends Migration
{
public function up()
{
Schema::create('reparations', function (Blueprint $table) {
$table->Increments('id_reparation');
$table->string('type_reparation')->nullable();
$table->string('date_reparation')->nullable();
$table->integer('prix_reparation')->nullable();
$table->string('genre_marque_type')->nullable();
$table->boolean('etat_reparation')->nullable();
/*$table->foreign('client_cin')->references('cin_client')->on('clients');*/
$table->timestamps();
$table->string('client_cin',30);
});
Schema::table('reparations', function (Blueprint $table){
$table->foreign('client_cin')->references('cin_client')->on('clients');
});
}
public function down()
{
Schema::dropIfExists('reparations');
}
}
So, that database error almost always means that the column you've defined to hold the foreign key reference in the first table, and the column you've defined in the second table are not matching formats or lengths. Looking at the migration (and not being sure what DBS you're using) I think you need to remove ->unsigned() from the column definition for reparations.client_cin.

Production migration fresh

How does it work if I want to add a new column in my production table? I know that I will loose all the data when I make a migration:fresh.
1) is it possible to change the migration and add a new column as I did with the fastnetnr column?
public function up()
{
Schema::create('kontaktforms', function (Blueprint $table) {
$table->increments('id');
$table->string('navn');
$table->string('mobilnr');
//new fastnetnr column added
$table->string('fastnetnr')->nullable();
$table->string('mail');
$table->string('emne');
$table->text('beskrivelse');
$table->timestamps();
});
2) Or do I have to add a new column with php artisan so the output is like this?
public function up()
{
Schema::table('kontaktforms', function($table) {
$table->string('fastnetnr')->nullable();
});
}
Logically you can do it by following steps;
rollback your table php artisan migrate:rollback or php artisan
migrate:rollback --step=1
edit your migration file
migration back php artisan migrate
but it's not a best practice, especially in production. so you have to create new migration file for modify table and migrate it.
create new migration file
php artisan make:migration add_fastnetnr_to_kontaktforms_table --table=kontaktforms
and add new column like this
public function up()
{
Schema::table('kontaktforms', function (Blueprint $table) {
$table->string('fastnetnr')->nullable();
});
}
public function down()
{
Schema::table('kontaktforms', function (Blueprint $table) {
$table->dropColumn('fastnetnr');
});
}
and migrate
php artisan migrate

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

Laravel 4.2 and migrate make not working

I create a project based on the book Getting Started with Laravel 4.
So, I create two files in app/models/ - Cat.php and Breed.php with this content:
Cat.php
<?php
class Cat extends Eloquent {
protected $fillable = array('name','date_of_birth','breed_id');
public function breed() {
return $this->belongsTo('Breed');
}
}
and Breed.php
<?php
class Breed extends Eloquent {
public $timestamps = false;
public function cats()
{
return $this->hasMany('Cat');
}
}
and after, I use command php artisan migration:make create_cats_and_breeds_table
Ok, and should arise file in app/database/migrations. It is.
But, its contents it's not same as in the book...
In book:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddCatsAndBreedsTable extends Migration {
public function up()
{
Schema::create('cats', function($table)
{
$table->increments('id');
$table->string('name');
$table->date('date_of_birth');
$table->integer('breed_id')->nullable();
$table->timestamps();
})
Schema::create('breeds', function($table)
{
$table->increments('id');
$table->string('name');
})
}
public function down()
{
Schema::drop('cats');
Schema::drop('breeds');
}
}
My code:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddCatsAndBreedsTable extends Migration {
public function up()
{
//
}
public function down()
{
//
}
}
What's happen?
https://github.com/laracasts/Laravel-4-Generators
Provides some additional artisan commands which you can used to specific your fields in order to generate the migration files.
php artisan generate:migration create_posts_table --fields="title:string, body:text"
migration:make command does not know anything about your models. It just creates a stub that you need to fill with column definitions for your tables.

ErrorException: Creating default object from empty value

I created table migrations in Laravel using php artisan migrate:make. When I tried to create the tables in the database, I got an error:
[ErrorException]
Creating default object from empty value
What is this related to? No tables are created nor can I find any errors in my migrations.
I have 25 tables in the migrations folder, all look similar to this.
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAddressesTable extends Migration {
public function up() {
Schema::create("addresses", function() {
$table->engine = "InnoDB";
$table->increments("id");
$table->integer("user_id")->unsigned();
$table->string("street");
$table->string("city");
$table->integer("postal_code")->unsigned();
$table->foreign("user_id")->references("id")->on("users");
$table->softDeletes();
$table->timestamps();
});
}
public function down() {
Schema::dropIfExists("addresses");
}
}
Well you miss $table that you pass to the function.
Your schema create function needs to be in this style...
Schema::create('addresses', function(Blueprint $table)

Categories