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)
Related
I am building management application using laravel. I am trying to create the tables 'role_user' in laravel, but when I run the 'php artisan migrate' command, I get the following: BadMethodCallException
Method Illuminate\Database\Schema\Blueprint::name does not exist. Can anyone tell what do you mean by this error?
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateRolesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->id();
$table->name();
$table->timestamps();
});
}
but if i tring to migrate that i see
λ php artisan migrate
Migrating: 2020_06_07_055653_create_roles_table
BadMethodCallException
Method Illuminate\Database\Schema\Blueprint::name does not exist.
at E:\laragon\www\os\vendor\laravel\framework\src\Illuminate\Support\Traits\Macroable.php:103
99| */
100| public function __call($method, $parameters)
101| {
102| if (! static::hasMacro($method)) {
> 103| throw new BadMethodCallException(sprintf(
104| 'Method %s::%s does not exist.', static::class, $method
105| ));
106| }
107|
• Bad Method Call: Did you mean Illuminate\Database\Schema\Blueprint::rename() ?
1 E:\laragon\www\os\database\migrations\2020_06_07_055653_create_roles_table.php:18
Illuminate\Database\Schema\Blueprint::__call("name", [])
2 E:\laragon\www\os\vendor\laravel\framework\src\Illuminate\Database\Schema\Builder.php:166
CreateRolesTable::{closure}(Object(Illuminate\Database\Schema\Blueprint))
Instead:
$table->name();
It supposed to be:
$table->string('name', 32); // change 32 to max length
Why?
There is no "name" type in MySQL (or any other DB engine). You probably was misleaded because of ->id() or ->timestamps(). Also there is no "id" column type, but this works because in Laravel this was made as shortcut (because it is often used).
So there is no difference if you use:
$table->id().
$table->timestamps();
Or
$table->bigIncrements('id');
$table->timestamp('created_at', $precision)->nullable();
$table->timestamp('updated_at', $precision)->nullable();
Because it is doing same thing under hood anyway.
Read more in docs.
You can change your code to :
Schema::create('roles', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
Change
$table->id()
to
$table->bigIncrements('id');
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.
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');
}
}
in Laravel using Schema Builder
how i can create a column and in the same time update his value whit the other value of the same table?
Its possible?
Thanks
Pseudo code
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddSkuToWaranties extends Migration {
public function up()
{
Schema::table('my_table_name', function(Blueprint $table) {
$table->text('my_table_column');
///////////////////////
$table->my_table_column = $table->my_other_table_column;
///////////////////////
});
}
public function down()
{
Schema::table('my_table_name', function(Blueprint $table) {
$table->dropColumn('my_table_column');
});
}
}
Yes that is possible. You can do it like this:
DB::table('my_table_name')
->update(array('my_table_column' => DB:raw('my_other_table_column')));
With this, you are using Laravel's Query Builder. It actually builds this query for you:
update my_table_name set my_table_column = my_other_table_column
Another solution is to just execute this raw query:
DB::update('update my_table_name set my_table_column = my_other_table_column');
As #lukasgeiter mentioned in a comment, you must place this code outside the closure, so this becomes the new up() method:
public function up()
{
Schema::table('my_table_name', function(Blueprint $table) {
$table->text('my_table_column');
});
DB::table('my_table_name')
->update(array('my_table_column' => DB:raw('my_other_table_column')));
}
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