php artisan make:migration not creaing new table - php

I have a table in my db named affilaite.
But when i try to create a new table named as product it gives me an error
php artisan make:migration create_product_table
PHP Fatal error: Cannot declare class CreateAffiliateTable, because the name is already in use in /home/manak/Desktop/manu/Edolve/database/migrations/2021_03_09_063908_create_affiliate_table.php on line 7
Symfony\Component\ErrorHandler\Error\FatalError
Cannot declare class CreateAffiliateTable, because the name is already in use
at database/migrations/2021_03_09_063908_create_affiliate_table.php:7
3▕ use Illuminate\Database\Migrations\Migration;
4▕ use Illuminate\Database\Schema\Blueprint;
5▕ use Illuminate\Support\Facades\Schema;
6▕
➜ 7▕ class CreateAffiliateTable extends Migration
8▕ {
9▕
10▕ Run the migrations.
11▕
Whoops\Exception\ErrorException
Cannot declare class CreateAffiliateTable, because the name is already in use
at database/migrations/2021_03_09_063908_create_affiliate_table.php:7
3▕ use Illuminate\Database\Migrations\Migration;
4▕ use Illuminate\Database\Schema\Blueprint;
5▕ use Illuminate\Support\Facades\Schema;
6▕
➜ 7▕ class CreateAffiliateTable extends Migration
8▕ {
9▕
10▕ Run the migrations.
11▕
+1 vendor frames
2 [internal]:0
Whoops\Run::handleShutdown()

From This error I Think U already have A migration named CreateAffiliateTable.
Please Change Your Migration name or Delete the Old migration

Check your database/migration folder. if a file already prosent there remove it. Also if there is already a table named exactly the same drop that table from database. Also in database there is a migrations table. Delete the tablename from there also and try migrating again.
By laravel convention migrations are plural and model name singular. So better try CreateAffiliatesTable instead of CreateAffiliateTable

use this command for create
$ php artisan make:migration create_products_table

Related

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'iop.servicecategories' doesn't exist (SQL: select * from `servicecategories`)

I have a problem with this error so there are my table and model
I try to put this in my model:
protected $table = 'servicecategories';
but the problem is not solved.
also try to clear cache and delete table but the problem is not solved,
also if i try to make a simple php artisan
Please run command => php artisan migrate
In db ..Your table name is different from your class name. I think they both should be the same I think this is the problem ..
When you run
php artisan migrate works?
Maybe it's because you used model or function other in AppServiceProvider. You need comment it before call migrate
you can use php artisan migrate:fresh to rewrite all your db based on existing migration code you have done
It means the database was created but the tables were not.
For Laravel applications, you need to migrate the tables like so ;
php artisan migrate
In your migration you are using a common class, but Laravel uses anonymous classes.
Replace
class CreateServicecategoriesTable extends Migration {
With
return new class extends Migration {

seeding table not sucess in Laravel 7 and Laratrust 6

using Laravel 7 and Laratrust 6 and need seeding the Table using the seed command but seeding only some tables only. not seeding following tables as well in the database users,role_user,permission_user
my DataBaseSeeder.php
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call(LaratrustSeeder::class);
}
}
how could I fix this problem?
add -> namespace Database\Seeders; in LaratrustSeeder.php in seeder folder

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.

Laravel Models Defining Columns Possible?

I just started playing with Laravel, and was wondering the following:
Can you define your table structure in the model, then create a migration based off it? (The way Django works).
namespace App;
use Illuminate\Database\Eloquent\Model;
class SomeListing extends Model
{
// Below V V V V Define Columns
public $title = ''; // somehow make a string(255)?
public $description = ''; // somehow make a text?
// etc
// etc
}
Or must you always adjust the migration files? I could not find this here.
I think it does not exist, but you can create migrations for your database with migrations
Create migration
php artisan make:migration create_table_name
and
php artisan migrate
More info could be found at Laravel's Migration Document

Categories