I am working on a laravel project and each time I change my table (add or remove column) and run php artisan migrate:refresh. I get this error:
[Symfony\Component\Debug\Exception\FatalErrorException] Can't use
method return value in write context
Solution tried:
run composer dump-autoload (Fails)
Drop table in database, delete the migration file and restart again (works)
Previous migration file:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('post_id');
$table->string('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
}
Changed migration file:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('post_id');
$table->string('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
}
I added the user_id in the change file in the up function
Try this command it works for me
php artisan migrate:fresh
However, be careful! This command will drop all data from your DB:
Note: The migrate:fresh command will drop all tables from the database and then execute the migrate command.
as per Laravel docs 9.x.
try this
fire this command
php artisan make:migration add_user_id_to_comments_table --table=comments
this will create a new migration file then
$table->integer('user_id')->after('id');
then use
php artisan migrate
Refresh the database and run all database seeds...
php artisan migrate:fresh --seed
Read Docs
Related
I am trying to create a model along with migration as follows:
php artisan make:model Candidate -m
The output is:
Model created successfully.
Created Migration: 2022_08_06_165641_create_candidates_table
Now on the migration file:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class {{ class }} extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('candidates', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('candidates');
}
}
Everything is fine except classname of migration file.
I tried deleting vendor folder, reinstalling composer packages and clearing config. But the problem still persists.
I'm learning Laravel and migrating a database table with
php artisan migrate
then I get an error
[Symfony\Component\Debug\Exception\FatalErrorException]
Call to undefined method Illuminate\Database\Schema\Blueprint::create_table()
Where does it come from?
Here is the code of the migration:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTodoTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
//
Schema::create('todos', function (Blueprint $table) {
$table->increments('id');
$table->string('text');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
Schema::dropIfExists('todos');
}
}
Laravel doesn't have create_table method. This means you've tried to use this name somewhere instead of using create.
So, search for create_table, change it to create. Then run composer du and php artisan migrate commands.
migrate generates the exception after pivot migration of Laravel 5.3.
schema1 : software_houses
schema2 : skill_domains
I generated the command make:migration:pivot skill_domains software_houses
and the migration is created by the name of skill_domain_software_house
but when I execute php artisan migrate
following error occurs :
[Symfony\Component\Debug\Exception\FatalThrowableError]
Class 'CreateSkillDomainSoftwareHousePivotTable' not found
See this Image
SKILL Domain
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSkillDomainsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('skill_domains', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('skill_domains');
}
}
Software House
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSoftwareHousesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('software_houses', function (Blueprint $table) {
$table->increments('id');
$table->string('name',100)->unique();
$table->string('desc',500);
$table->string('url',100);
$table->string('rating')->default('3');
$table->string('logo')->nullable();
$table->string('city')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('software_houses');
}
}
Pivot table created after running the make:migration::pivot command
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSkill_domainSoftware_housePivotTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('skill_domain_software_house', function (Blueprint $table) {
$table->integer('skill_domain_id')->unsigned()->index();
$table->foreign('skill_domain_id')->references('id')->on('skill_domains')->onDelete('cascade');
$table->integer('software_house_id')->unsigned()->index();
$table->foreign('software_house_id')->references('id')->on('software_houses')->onDelete('cascade');
$table->primary(['skill_domain_id', 'software_house_id']);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('skill_domain_software_house');
}
}
Try with below class name line:
class SkillDomainsSoftwareHouses extends Migration
Make sure the file name should be match with class name Documentation.
UPDATE
I think you should try this:
first remove above software_houses,skill_domains from migrations folder also remove from migrations table in your database then follow below steps :
1) php artisan make:migration skilldomain
2) php artisan make:migration softwarehouses
3) php artisan make:migration:pivot skilldomains softwarehouses
4) php artisan migrate
I am having a problem in Laravel 5.3.
It won't allow me to drop a column from an existing table. I have run 'composer require doctrine/dbal' and that worked fine, but my column will not delete.
My add_column_to_table code:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddColumnToTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table)
{
$table->string('avatar')->default('default.pngs');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('users', function ($table)
{
$table->dropColumn('avatar');
});
}
}
Thanks
To drop the column you need to rollback the migration in the following manner
php artisan migrate:rollback
Try this:
public function down()
{
Schema::table('users', function (Blueprint $table)
{
$table->dropColumn('avatar');
});
}
}
You should rollback your migration:
php artisan migrate:rollback
But make sure that column is empty on your table.
When I run php artisan migrate i get error B
Base table or view already exists: 1050 Table 'categories' already
exists'
What is it? Why? How to find error?
My categories migrations file:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CategoriesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('title')->index();
$table->text('description');
$table->integer('attachment_id')->unsigned()->index();
$table->foreign('attachment_id')->references('id')->on('attachment')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
In that migration file, the function drop() should be
public function down()
{
Schema::drop('categories');
}
It seems that you try to create existing table..
so i think you are editing the migration file that you already installed
you can drop your database and re-migrate it
php artisan migrate:reset
then
php artisan migrate