Laravel 5.0 - Rename multiple columns in 1 migration - laravel-5

We recently made a switch of service provider such that multiple columns in a DB table for this project will need to be renamed.
I am aware of this post which shows how to rename 1 column from 1 table:
php artisan migrate:make rename_stk_column --table="YOUR TABLE" --create
Is there a way to execute this same migration with multiple columns ? (1 migration, not more than 1...trying to minimize number of migration files created)

You can just add multiple renameColumn(); statements for each column that needs to be updated in that given table. Just need to come up with a whatever name you guys/gals use for your migration files.
Just a sample of what I ran
class MultipleColumnUpdate extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function ($table) {
$table->renameColumn('name', 'user_name');
$table->renameColumn('email', 'work_email');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('users', function ($table) {
$table->renameColumn('user_name', 'name');
$table->renameColumn('work_email', 'email');
});
}
}

Related

How to do hasMany and belongsToMany at same model?

I have 2 models, Employee & FieldReport. I need to create relations based on the following conditions:
Field report is owned by an employee whose character is absolute
(owner's data must be displayed and cannot be edited), where the
report field also has a tag to mark who the employees are in that
report field.
An employee, himself, has many field reports.
For now, I've made a relationship, something like this:
Employee has many Field Reports.
Employee belongs to many Field Reports.
Field Report belongs to Employee.
Field Report belongs to many Employees.
Then I have a problem where PHP doesn't allow the same method name (in the Employee model).
Example:
Has many has the method name fieldReports ()
Belongs to many also have the method name fieldReports ()
Whereas if I define the function name custom, I cannot get the value to fill the first pivot column and generate an error like the following:
SQLSTATE [23000]: Integrity constraint violation: 19 NOT NULL
constraint failed: field_report_participant.field_report_id (SQL:
insert into "field_report_participant" ("id", "participant_id") values
​​(1, 2))
Is there any solution? This is how my scripts looks like:
Employee.php
/**
* Each employee has many fieldReports.
*
* #return \Illuminate\Database\Eloquent\Relationship\HasMany
*/
public function fieldReports()
{
return $this->hasMany(FieldReport::class);
}
/**
* Each employee belongs to many fieldReports.
*
* #return \Illuminate\Database\Eloquent\Relationship\BelongsToMany
*/
public function fieldReports()
{
return $this->belongsToMany(FieldReport::class);
}
FieldReportController.php
/**
* Store a newly created resource in storage.
*
* #param \App\Http\Requests\RequestFieldReport $request
* #return \Illuminate\Http\Response
*/
public function store(RequestFieldReport $request)
{
$fieldReport = $this->data($request, $this->storeImages($request));
$fieldReport->participants()->sync(
$request->participants
);
return response()->json([
'created' => true,
'data' => $fieldReport,
], 201);
}
FieldReport.php
/**
* Each field report belongs to a company.
*
* #return \Illuminate\Database\Eloquent\Relationship\BelongsTo
*/
public function company()
{
return $this->belongsTo(Company::class);
}
/**
* Each field report belongs to a employee.
*
* #return \Illuminate\Database\Eloquent\Relationship\BelongsTo
*/
public function employee()
{
return $this->belongsTo(Employee::class);
}
/**
* Each field report belongs to many participants.
*
* #return \Illuminate\Database\Eloquent\Relationship\BelongsToMany
*/
public function participants()
{
return $this->belongsToMany(Employee::class, 'field_report_participant', 'participant_id', 'id');
}
create_field_reports_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateFieldReportsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('field_reports', function (Blueprint $table) {
$table->id();
$table->bigInteger('company_id');
$table->bigInteger('employee_id');
$table->string('title', 100);
$table->text('chronology');
$table->json('images')->nullable();
$table->timestamp('closed_at')->nullable();
$table->string('closed_by', 100)->nullable();
$table->timestamp('opened_at')->nullable();
$table->string('opened_by', 100)->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('field_reports');
}
}
field_report_participant_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateFieldReportParticipantTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('field_report_participant', function (Blueprint $table) {
$table->id();
$table->bigInteger('field_report_id');
$table->bigInteger('participant_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('field_report_participant');
}
}
After an hour pulling off my hair, trying to do a backflip and asked on each different forums, finally I had the answer. Unfortunately, he has no account on this forum and can't give the answer for this question.
The problem is I put a wrong key on the participants method which causing the field_report_id placed in a wrong place, in this case; id. Which is solved by doing this:
/**
* Each field report belongs to many participants.
*
* #return \Illuminate\Database\Eloquent\Relationship\BelongsToMany
*/
public function participants()
{
return $this->belongsToMany(Employee::class, 'field_report_participant', 'field_report_id', 'participant_id');
}
And then, on the Employee class, I could create exactly different method and link it with the pivot table. Like this:
/**
* Each employee belongs to many assignedFieldReports.
*
* #return \Illuminate\Database\Eloquent\Relationship\BelongsToMany
*/
public function assignedFieldReports()
{
return $this->belongsToMany(FieldReport::class, 'field_report_participant', 'participant_id', 'field_report_id');
}
Hopefully, it can help someone facing this exact same issue on the future.

Password_resets table missing, even after doing php artisan migrate

I am trying to do password recovery in laravel, but after inserting an email to send the reset request, an error appears saying that password_resets doesn't exist.
I've already tried to migrate again but nothing works.
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "password_resets" does not exist
LINE 1: delete from "password_resets" where "email" = $1 ^ (SQL: delete from "password_resets" where "email" = blabla#gmail.com)
According to this, it seems that the command to generate the migration for password_resets no longer exists, you can try to create a new migration with this:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePasswordResetsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('password_resets', function (Blueprint $table) {
$table->string('email')->index();
$table->string('token')->index();
$table->timestamp('created_at');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('password_resets');
}
}
In my case, I fixed the issue of the missing table by adding these two lines in AppServiceProvider.php.
Follow these steps:-
Open AppServiceProvider.php (Location: app/Providers/AppServiceProvider.php).
Add this line outside from classes use Illuminate\Support\Facades\Schema;
Add this line inside function boot() Schema::defaultStringLength(191);
Delete all tables from database.
Run this command php artisan migrate
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
}

Laravel does not create a record in `migrations` table on refresh

I have an awkward problem, every second time I run php artisan migrate:refresh --seed the migrations table is empty, though it should have initial migration record added after migration: 2018_05_02_114819_add_initial_migration
Here is my migration code (it is an import of old database schema):
<?php
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddInitialMigration extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
if(!Schema::hasTable('users')){
DB::unprepared(File::get(database_path('sam.sql')));
}
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
DB::statement('SET FOREIGN_KEY_CHECKS=0');
foreach (DB::select('SHOW TABLES') as $table) {
$table_array = get_object_vars($table);
if($table_array[key($table_array)] !== 'migrations'){
DB::statement('DROP TABLE ' . $table_array[key($table_array)]);
}
}
DB::statement('SET FOREIGN_KEY_CHECKS=1');
}
}
I found it out. It seems that in .sql file there was a line that said:
SET AUTOCOMMIT = 0;
Which was not set back to 1 in the end of the file.

Syntax error or access violation:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; check the manual that corresponds to your
MariaDB server version for the right s yntax to use near '&beauty
CHANGE id id INT AUTO_INCREMENT NOT NULL' at line 1
Hi there,
i have met a problem in my laravel project, when i tried to edit my column by migration.
I just need to change length of 'id' in table.
Here is my code`
class FieldChange extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('health&beauty', function (Blueprint $table) {
$table->integer('id', 11)->change();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
You have to use single `` for your table name like this.
`health&beauty`

neo4j migration in laravel doesnot work properly

I am using Neo4j in my application with /Vinelab/NeoEloquent. Now the problem is that when I run php artisan neo4j:migrate --database=neo4j
, node for the migration table is created.
Not any labels defined in function up() is not created. What should I do to solve this?
Schema class is,
<?php
use Vinelab\NeoEloquent\Schema\Blueprint;
use Vinelab\NeoEloquent\Migrations\Migration;
class CreateTestpostsTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Neo4jSchema::label('Testposts', function(Blueprint $label)
{
$label->unique('tpid');
$label->index('name');
$label->index('content');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Neo4jSchema::drop('Testposts');
}
}
According to your Schema code, no labels should be expected to be created. You are setting a constraint on the tpid to be unique, and indexing the name and content of the nodes with Testposts label.
To check if your migration has taken effect:
Try creating two nodes with the same tpid and you should get an error
Try profiling the queries to the name and content properties, this link should help with that: http://neo4j.com/docs/stable/cypherdoc-basic-query-tuning-example.html
On a side note, if the Label of the node in your model is Post or anything other than Testposts, then you should change Testposts to whatever is in your model for this to work properly.

Resources