Parse Error {} php Migrations - php

I am getting a parse error on line 50 which is the closing brace for my schema create statement however I cannot see any missing syntax so I am confused.
Code:
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('user_id');
$table->string('f_name');
$table->string('l_name');
$table->string('gender');
$table->date('dob');
$table->string('company_name');
$table->string('email')->unique();
$table->string('password');
$table->increments('landline');
$table->increments('mobile');
$table->increments('activated');
$this->increments('social_login');
$table->timestamp('last_login');
$table->rememberToken();
}
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('users');
}
}

Schema::create(
You never closed that (.
Schema::create('users', function (Blueprint $table) {
$table->increments('user_id');
$table->string('f_name');
$table->string('l_name');
$table->string('gender');
$table->date('dob');
$table->string('company_name');
$table->string('email')->unique();
$table->string('password');
$table->increments('landline');
$table->increments('mobile');
$table->increments('activated');
$this->increments('social_login');
$table->timestamp('last_login');
$table->rememberToken();
});

You are using the increments method incorrectly - Laravel will attempt to make an auto-incrementing primary key for each of the $table->increments(...); statements. Even the fields being specified would not suggest that an auto-incrementing field would be appropriate, you should check out the available methods in the Laravel docs

Related

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'rbuckridge#example.com' for key 'users_email_unique'

Here is the migration code:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('first_name');
$table->string('last_name');
$table->text('bio');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('profile_picture');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
The migration has less contribution to the error you are getting. This because at this point the error indicates that the rbuckridge#example.com is already existaing with the user table. This is achieved with the ->unique() method which prevent same data being re-entered in the same table column.
My suggestion is try to clear all the data first in the table and re enter the mail or use another different mail.

Can't add foreign key Laravel

I have 2 tables (regions & categories) , I wanted to have a foreign key in regions table this is my migration file :
public function up()
{
Schema::create('regions', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('nom');
$table->text('description');
$table->integer('vote');
$table->string('securite');
$table->integer('idCat')->unsigned()->change();
$table->foreign('idCat')->references('idCat')->on('categories');
$table->timestamps();
});
}
the table 'categorie' looks like this :
public function up()
{
Schema::create('categories', function (Blueprint $table) {
//$table->engine = 'InnoDB';
$table->increments('idCat');
$table->String('description');
$table->timestamps();
});
}
But I got this error : Syntax error or access violation: 1072 Key column 'idCat'
doesn't exist in table (SQL: alter table regions add constraint regions_
idcat_foreign foreign key (idCat) references categories (idCat))
Canyou help me please ?
Make sure you get rid of the change(), the column is not there yet.. The following code works perfectly for me...
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class Test extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
//$table->engine = 'InnoDB';
$table->increments('idCat');
$table->String('description');
$table->timestamps();
});
Schema::create('regions', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('nom');
$table->text('description');
$table->integer('vote');
$table->string('securite');
$table->integer('idCat')->unsigned();
$table->foreign('idCat')->references('idCat')->on('categories');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('regions');
Schema::drop('categories');
}
}

Call to undefined method Illuminate\Database\Schema\MySqlBuilder::dropForeign()

class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->text('comment');
$table->boolean('approved');
$table->integer('post_id')->unsigned();
$table->timestamps();
});
Schema::table('comments', function (Blueprint $table) {
$table->foreign('post_id')->references('id')->on('posts');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropForeign(['post_id']);
Schema::dropIfExists('comments');
}
}
This is what my migration class looks like, i have been trying to delete the table from the database but it throws me an error.
Error
Call to undefined method Illuminate\Database\Schema\MySqlBuilder::dropForeign()
I have gone through the documentation but it doesnot seem to be of much help.
could anyone please point out my mistake and what would be the solution ?
Just so you know , i am new to laravel.Go easy on me.Thanks!.
dropForeign needs to be called under Schema::table with a Blueprint object,
Schema::table('comments', function (Blueprint $table) {
$table->dropForeign('comments_post_id_foreign');
});
This follows the naming convention of <table_name>_<foreign_table_name>_<column_name>_foreign.
OR
Schema::table('comments', function (Blueprint $table) {
$table->dropForeign(['your_key_name']);
});
You're using wrong syntax. It should be:
Schema::table('comments', function (Blueprint $table) {
$table->dropForeign('comments_post_id_foreign');
});
https://laravel.com/docs/5.3/migrations#foreign-key-constraints

Laravel 5.2 Foreign Key fails

I am trying to add a foreign key in my posts table to reference the id in the users table. Here are my migrations, the users table is the default one packed with laravel so the timestamps are from NOV 2014 however the posts is one I created. Any help greatly appreciated.
Thanks a lot :)
Users migration
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('name')->unique();
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('users');
}
}
posts migration
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('title');
$table->string('section');
$table->string('body');
$table->date('post_created_date');
$table->integer('author_id')->unsigned()->after('id');
$table->foreign('author_id')->references('id')->on('users');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('posts');
}
}
Every time I migrate I get the following error:
If you are creating a table, you don't want to use after as it will create a syntax error. Just place the column where you want it. I believe after will only work for alter table statements.
Schema::create('posts', function (Blueprint $table) {
$table->increments('id'); // Unsigned is assumed for increments so I removed that as well
$table->integer('author_id')->unsigned();
$table->string('title');
$table->string('section');
$table->string('body');
$table->date('post_created_date');
$table->timestamps();
$table->foreign('author_id')->references('id')->on('users');
});
Then in your model, you'd setup the relationship to your users table like so...
public function author()
{
return $this->belongsTo(User::class, 'author_id');
}
Feel free to rename the function however you want, I usually prefer to name it something that matches the foreign key.
Then you'd use it like $post->author->name.
In your CreatePostsTable class, can you try this ?
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('author_id')->unsigned();
$table->string('title');
$table->string('section');
$table->string('body');
$table->date('post_created_date');
$table->timestamps();
});
Schema::table('posts', function($table) {
$table->foreign('author_id')->references('id')->on('users');
});
}

SQLSTATE[42000]: Syntax error or access violation: 1064 default character set utf8 collate utf8_unicode_ci' at line 1 [duplicate]

This question already has answers here:
Syntax error or access violation error while running "php artisan migrate" for migration file containing primary & foreign keys
(2 answers)
Closed 1 year ago.
I'm trying to migrate this code into mysql database but keep getting this error message.
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near ') default
character set utf8 collate utf8_unicode_ci' at line 1
public function up()
{
Schema::create('user', function(Blueprint $table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
$table->integer('projectId')->unsigned();
$table->boolean('isStudent');
$table->boolean('isCompany');
$table->String('img');
});
Schema::create('user', function($table)
{
$table->foreign('projectId')->references('id')->on('project');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('user');
}
}
For the second instance, use Schema::table
For more information, see here: https://stackoverflow.com/a/28007930/938664
public function up()
{
Schema::create('user', function(Blueprint $table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
$table->integer('projectId')->unsigned();
$table->boolean('isStudent');
$table->boolean('isCompany');
$table->string('img');
$table->foreign('projectId')->references('id')->on('project')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('user');
}
}
try this Schema::table
public function up()
{
Schema::create('user', function(Blueprint $table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
$table->integer('projectId')->unsigned();
$table->boolean('isStudent');
$table->boolean('isCompany');
$table->String('img');
});
Schema::table('user', function($table)
{
$table->foreign('projectId')->references('id')->on('project');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('user');
} }

Categories