Laravel 4 Migration Error - php

I'm getting the following error when trying to create migrations for my laravel 4 installation. The file gets created but it outputs the following error.
Created Migration: 2014_07_06_073213_create-users-table
Generating optimized class loader
Compiling common classes
{"error":
{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Class 'ClassPreloader\\Command\\PreCompileCommand' not found","file":"\/home\/name123\/domain.com\/laravel\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Console\/OptimizeCommand.php","line":113}}[warehouse]
$ php artisan migrate:make create-users-table
It doesn't seem that there are any other people having the same problem.
Also getting this error when i commit the migration
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Call to a member function increments() on a non-object","file":"\/home\/dandel26\/danieldelcore.com\/laravel\/app\/database\/migrations\/2014_06_29_092641_create_users_table.php","line":15}}
Thanks in advance.

In you're migration you have the function up. In this function there will be something like:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
});
It would seem that you did not do define the callback with the variable $table. Please add Blueprint $table and it should work.
In that case remember to import the right namespaces:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

Related

Not enough arguments missing name when i make seed Laravel

i am trying to make a seed with:
php artisan make:seed
But, i get the error:
Not enough arguments (missing: "name").
I dont know why, this is the seed i am trying to make:
class ProfessionSeeder extends Seeder
{
public function run()
{
DB::table('professions')->insert([
'title' => 'Desarrollador Back End'
]);
}
}
This is the migration corresponding to the DB table...
class CreateProfessionsTable extends Migration
{
public function up()
{
Schema::create('professions', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title', 100);
$table->timestamps();
});
}
Any ideas of what is going on?
Thank you!
You can see what is required for a command by using artisan help:
php artisan help make:seed
Will output something like:
Description:
Create a new seeder class
Usage:
make:seeder <name>
Arguments:
name The name of the class
...
This command takes an argument for the name of the class you want to create.
php artisan make:seed ProfessionSeeder

Why does php artisan create migration class with empty up method?

I am currently learning from this video
https://laracasts.com/series/laravel-from-scratch-2018/episodes/7
about database migrations in laravel.
I typed in console :
php artisan make:migration create_projects_table
just like the teacher in the video and somehow I get an empty method definition in the migrations folder.
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProjectsTable extends Migration
{
public function up()
{
//should contain Schema::create but it is empty
}
public function down()
{
}
}
What did I do wrong? I followed all those instructions in the video.
You can specify the table:
php artisan make:migration create_projects_table --create=projects
edit: It should indeed work without adding the argument:
https://github.com/laravel/framework/blob/854e6d1d001f5e9a6d1376d2284eaa99b0c1e443/src/Illuminate/Database/Console/Migrations/MigrateMakeCommand.php#L88
// Next, we will attempt to guess the table name if this the migration has
// "create" in the name. This will allow us to provide a convenient way
// of creating migrations that create new tables for the application.
if (! $table) {
[$table, $create] = TableGuesser::guess($name);
}
The tableGuesser class
Here you can see that your tables name should match (\w+) pattern
Use this command on root folder
php artisan make:migration create_projects_table --create=projects //create only Migration file
php artisan make:model Project -m //Create Migration, Model file
php artisan make:model Project -mcr //For Create Migration,Model,Controller file

Error migrations: Cannot declare class X, because the name is already in use

I do not know why this error occurs when I execute the migrations as I do not have repeated classes.
Migrations:
2014_10_12_100000_create_password_resets_table.php
2019_01_18_020910_create_roles_table.php
2019_01_18_025535_create_members_table.php
2019_01_18_025536_create_users_table.php
2019_01_18_183649_create_projects_table.php
2019_01_18_184249_create_member_project_table.php
2019_01_18_184719_create_sprints_table.php
2019_01_18_185218_create_tasks_table.php
2019_01_21_033045_add_shortname_to_project.php
Error:
PHP Fatal error: Cannot declare class CreateRolesTable, because the name is already in use in
oyectos\database\migrations\2019_01_18_020910_create_roles_table.php on line 33
In 2019_01_18_020910_create_roles_table.php line 33:
Cannot declare class CreateRolesTable, because the name is already in use
Class:
class CreateRolesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name',128)->unique();
$table->string('description');
$table->boolean('system');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('roles');
}
}
As well as other answers given, this error can also happen if the migration filename is not a snake-case version of the class name.
So a migration file 2019_01_18_020910_create_roles_table.php must contain the class CreateRolesTable. If it contains the class CreateRoleTable, with a missing s, then the "Cannot declare X..." error is thrown. I've found this on Laravel 8, and may apply to earlier versions.
It appears to happen because Laravel loads the migration file multiple times when the filename is misspelled, and the second time loading is when the exception is throw.
First Solution :
It seems like you have 2 migrations done at different time with essentially same name.
for example : 2019_01_18_020910_create_roles_table.php
and 2019_01_16_020910_create_roles_table.php
Laravel will convert this filename eliminating the date signature and Camel Casing the remaining text.
So both of these migration will have class CreateRolesTable even if the time signatures are different. Check if your migrations directory have such 2 files.
To check this run this from terminal in project root : grep -ri 'createrolestable' database/migrations
Second Solution :
Sometimes composer's internal class autoloading causes this issue. Do following to check if it resolves :
run composer install
Third Solution :
This is likely to be invalid but a same file should not have same class declaration 2 files by mistake.
Fourth Solution :
There might be a package you have installed which has a migration with same class name. To find run grep -ril 'createrolestable' vendor
If it shows any file then thats what causing 2 classes to have same names.
You can create a new one php artisan make:migration create_roles_table_custom . and then copy what you have in current migration to the new one and delete the existing one(not from package but the one you have created).
This will create a class CreateRolesTableCustom which is different than what the package already has.
If you are using Laravel 8 or above, you can use Anonymous Migration to avoid the conflict with Class name.
Below is how you to declare an Anonymous Migration. Do not forget the semicolon at the end.
return new class extends Migration
{
//
};
More from the Docs.
Becareful of migration file name.
For me, migration file name was:
2021-10-13_000000_create_examples_table
But correct was :
2021_10_13_000000_create_examples_table
LOL
In my case, i had my own package, which had migraration and it wasn't named properly. I named it without date like this: create_orders_table.
I changed it to 2021_08_03_000000_create_orders_table and it helped.
I ran into this (misleading) error and it turned out I accidently omitted the word Create from the migration class name.
Error: Cannot declare class FooTable, because the name is already in use
Incorrect: class FooTable extends Migration
Correct: class CreateFooTable extends Migration
For me, it was a problem with Laravel Sanctum (now built into Laravel 8). I had generated migrations via a package and somehow ended up with something in vendor\laravel\sanctum\database\migrations.
I ran php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider" to persist it to standard migrations.
See here for details.
I had this problem. I used composer dump-autoload and it solved the problem.
Even if you don't have any such files with same Class name and you are still facing the same problem then try
composer dump-autoload
This can be caused by a number of things. Follow these steps to resolve the issue.
First run this command in terminal
php artisan optimize:clear
composer dump-autoload
If those don't resolve the issue, then you have renamed a migration file that was published from Laravel Cashier. To resolve it, do the following:
Rename the migration file. Something like 2019_01_18_020910_create_roles_table can be renamed to 2019_01_18_020910_create_role_table
Rename the class. Something like CreateRolesTable can be renamed to CreateRoleTable
in my case, it's throwing the error because I changed the time of migration file which are added by laravel cashier, my time sequence is correct but it's still throwing name issue.
Then I revert the time back to the original migration time and the problem is solved.

How do I change the automatically generated migration page in Laravel?

When I genarate a migration in Laravel, it automatically looks like this:
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
But I want to work more convenient, I want to let the database handle when I create and update a row instead of doing it myself everytime.
I found a way to make this possible like this:
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
});
}
But now everytime I make a migration, I manually have to change this and I don't think that's very convenient. Does anybody know how I can change the automatically generated migration in Laravel?
If you dig around in the source code you will find that:
There's a file called create.stub in the framework.
This file is used by the MigrationCreator to make migrations.
In principle you can do the following:
Grab the built-in migration file and move it in another folder in your project (e.g. resouces/stubs probably) Note that you should copy the other stubs in that folder too even if you won't modify them.
Then, override the default migration creator to use this file instead, this should work:
class MyMigrationCreator extends MigrationCreator {
protected function stubPath() {
return base_path("resources"); //Or something valid
}
}
Then in your application service provider you can do:
$this->app->instance(MigrationCreator::class, resolve(MyMigrationCreator::class));
This will (hopefully) "trick" laravel into using your migration creator than the default one. However, creating tables is not something that happens so often to justify all this trouble.
Update: It should extend the migration creator.
In Laravel 5.6 there seems to be no possibility to override MigrationCreator class because it is used directly in MigrationServiceProvider:
protected function registerCreator() {
$this->app->singleton('migration.creator', function ($app) {
return new MigrationCreator($app['files']);
});
}
But you can hack MigrationServiceProvider next way:
Create directory in your project root: overrides/Illuminate/Database
Copy file vendor/laravel/framework/src/Illuminate/Database/MigrationServiceProvider.php into overrides/Illuminate/Database folder (note that class namespace will remain same)
Modify overrides/Illuminate/Database/MigrationServiceProvider.php:
protected function registerCreator() {
$this->app->singleton('migration.creator', function ($app) {
return new MyMigrationCreator($app['files']);
});
}
Modify your composer.json ("Illuminate\\"):
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/",
"Illuminate\\": "overrides/Illuminate"
}
},
Run composer dump-autoload. Composer will say
`Warning: Ambiguous class resolution, "Illuminate\Database\MigrationServiceProvider" was found in both "$baseDir . '/overrides/Illuminate/Database/MigrationServiceProvider.php" and "/vendor/laravel/framework/src/Illuminate\Database\MigrationServiceProvider.php", the first will be used.`
Now your fake MigrationServiceProvider will be used instead of Laravel's but you won't be able to use original one. That's why we copied whole file.
Actually this way you can override MigrationCreator class but amount of code there is high and you actually will need to extend it with your MyMigrationCreator class and override several methods like stubPath() and maybe create(). But MigrationServiceProvider can be quite safely overriden because it contains several small methods that are unlikely will be changed until Laravel 6

Laravel 5 migration refuses to run

I'm growing desperate, I need help! I've been at this all day!
I want to configure a DB in Laravel. I have all my migrations (in order) so no foreign key will conflict and hit a table that would only be created later.
But no matter what I try I keep hitting an error that says this,
http://prntscr.com/6z23nw
EDIT: now it says the exact same thing but that the class was 'not found'... I'm totally clueless here....
A bit more information, (I was going to post a screenshot but I can't)... searching for usages of GroupUser all I can find is the one I posted and entries in the laravel.log (listed as non-code usage). So there really is no other "groupUser" class
Here's a screenshot of my migrations folder.
These are my migrations they run in order and all tables until GroupUser are properly created
the migration that's causing all this mess
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class GroupUser extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('group_user', function(Blueprint $table)
{
$table->integer('user_id')->unsigned();
$table->integer('group_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('group_id')->references('id')->on('groups');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('group_user');
}}
One final note I completely erased all the previous migrations and even tried reinstalling with the php artisan migrate:install
Run composer dump-autoload. After creating the migration files via artisan, you need to re-run composer to have them be autoloaded automatically.
Solved the issue,
. migrate:reset to set everything from blank
. created migrations with the tables but no foreign keys and dependencies
. php artisan migrate (all worked out fine)
. created new migrations altering the tables required with the foreign keys.
. again 'php artisan migrate'
I think it all worked fine. Thank you for your help and ideas everyone!

Categories