Syntax error or access violation: - laravel

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`

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);
}
}

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.

Laravel 5.0 - Rename multiple columns in 1 migration

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');
});
}
}

Laravel Migrations gives unusual error

I am trying to create a migration using artisan. The migrations class is created without any problems and I construct the table using the Schema class. When I run its. It works all good, but when I try and rollback, It gives an error which I don't understand.
My Migration Class
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class Session extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('Session', function(Blueprint $table){
$table->string('id', 50);
$table->string('email', 100);
$table->integer('lastActivity');
$table->primary('id');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('Session');
}
}
Error message
[2014-03-25 14:42:16] production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Class 'Session' not found' in E:\Documents\Dropbox\Documents\WorkSpace_netBeans\Laravel_Test\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php:297
Your Session migration class might be in conflict with Laravel's Session (Facade), rename it to test.
I just created one here:
php artisan migrate:make Session
And it conflicted while trying to rollback:
PHP Fatal error: Call to undefined method Illuminate\Support\Facades\Session::down() in

Resources