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');
Related
I'm new to Laravel and php so I face and error I don't know how to solve.
The basic problem is since a lot of tables have primary:id and created_by,updated_by columns what I've figured out is inheriting them in my migrations.
I'm using php7
So I have a Base class
class BaseMigration extends Migration {
public function up(string $tableName) {
Schema::create($tableName, function (Blueprint $table) {
$table->mediumIncrements('id');
$table->primary('id');
$table->unsignedMediumInteger('created_by')->references('id')->on('users');
$table->unsignedMediumInteger('updated_by')->references('id')->on('users');
});
}
}
and the extending migration
class CreateItemsTable extends BaseMigration {
public function up() {
parent::up('items');
Schema::create('items', function (Blueprint $table) {
$table->string('name', 74);
$table->timestamps();
});
}
// ......
}
However php artisan migrate gives me this:
[ErrorException] Declaration of CreateItemsTable::up() should be compatible with Illuminate\Database\Migrations\BaseMigration::up(string $tableName)
Is it because I'm running double up() ?
What am I missing? Appreciate your kind help.
I think you need to have the same function signature, so pass string $tableName:
class CreateItemsTable extends BaseMigration {
public function up(string $tableName) {
Schema::create('items', function (Blueprint $table) {
parent::up('items');
$table->string('name', 74);
$table->timestamps();
});
}
// ......
}
Here is Vehicles migrate :
public function up()
{
Schema::create('Vehicles', function (Blueprint $table) {
$table->increments('serie');
$table->string('color');
$table->integer('power');
$table->float('capacity');
$table->float('speed');
$table->integer('maker_id')->unsigned();
$table->timestamps();
});
Schema::table('Vehicles', function (Blueprint $table){
$table->foreign('maker_id')->references('id')->on('Makers');
});
}
Here is Makers Migrate :
public function up()
{
Schema::create('Makers', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('phone');
$table->timestamps();
});
}
When I run artisan migrate , I got following error messages.
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table `api`.`#sql-14b0_71
` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter t
able `Vehicles` add constraint `vehicles_maker_id_foreign` foreign key (`ma
ker_id`) references `Makers` (`id`))
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table `api`.`#sql-14b0_71
` (errno: 150 "Foreign key constraint is incorrectly formed")
I want to create two tables: Vehicles & Makers . In vehicles , maker_id is foreign key . I read the some references for laravel migration from various sources. But I can't find solution.
Pay attention to your migration files order, you have two solutions:
Change order: make Makers Migrate file first then Vehicles Migrate.
If Makers Migrate file comes after Vehicles Migrate file and you don't want to change order, move this:
Schema::table('Vehicles', function (Blueprint $table){
$table->foreign('maker_id')->references('id')->on('Makers');
});
To Makers Migrate file.
Now I delete makers migrate file and I run vehicles migrate file . Below is vehicles migrate file.
public function up()
{
Schema::create('Vehicles', function (Blueprint $table) {
$table->increments('serie');
$table->string('color');
$table->integer('power');
$table->float('capacity');
$table->float('speed');
$table->integer('maker_id')->unsigned();
$table->timestamps();
});
}
But I got the below errors
PHP Fatal error: Cannot redeclare class CreateVehicleTable in C:\xampp\htdocs\m
yownapi\database\migrations\2016_07_05_190215_vehicles_table.php on line 35
[Symfony\Component\Debug\Exception\FatalErrorException]
Cannot redeclare class CreateVehicleTable
Now it is work properly with below codes
Makers Migrate
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class MakersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('Makers', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('phone');
$table->timestamps();
});
Schema::table('Vehicles', function(Blueprint $table){
$table->foreign('maker_id')->references('id')->on('makers');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('Makers');
}
}
Vehicles Migrate
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class VehiclesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('Vehicles', function (Blueprint $table) {
$table->increments('serie');
$table->string('color');
$table->float('power');
$table->float('capacity');
$table->integer('maker_id')->unsigned();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('Vehicles');
}
}
There is a simple way to do this. From your Vehicles migration, make the following changes
public function up()
{
Schema::create('Vehicles', function (Blueprint $table) {
$table->increments('serie');
$table->string('color');
$table->integer('power');
$table->float('capacity');
$table->float('speed');
$table->integer('maker_id')
$table->foreign('maker_id')
->references('id')->on('Makers');
$table->timestamps();
});
}
And since you will be adding the values form the id column in Makers, Laravel already has got your back, so there is really no need to add the unsigned() property there. Its still Okay if you add it there.
Hope I have answered your question. For More Details
there. I'm trying to deal with Laravel Framework, but there is a problem. Here is my code
public function up()
{
Schema::create('users', function($table)
{
$table->increments('id');
});
}
I made migration and tried to add some fields to my table. But IDE doesn't recognize $table variable properly, so as a result I have warning: "Method increments doesn't found in class", and I can't use auto-completion.
Are there propositions how to fix this?
Whole code:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration {
public function up()
{
Schema::create('posts', function($table)
{
$table->increments('id');
$table->string('title',150);
$table->text('body');
$table->string('preview',300);
$table->string('author',100);
$table->timestamps();
});
}
public function down()
{
Schema::drop('posts');
}
}
Just specify the type of $table. The class is Illuminate\Database\Schema\Blueprint
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
});
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)
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