When I run php artisan migrate, I am getting an error like this:
In 2017_12_26_045926_create_table_articles.php line 41:
Parse error: syntax error, unexpected 'public' (T_PUBLIC), expecting ',' or
')'
This is my articles tables:
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('category_id');
$table->string('title');
$table->text('content');
$table->boolean('is_show')->default(false);
$table->boolean('is_active')->default(false);
$table->integer('page_showing')->default(0);
$table->string('header_pic');
$table->softDeletes();
$table->timestamps();
Schema::table('articles', function($table){
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
}
I am adding foreign key for articles and comments, but the articles tables when migrate is giving errors like above. What's wrong?
The error is because you are using the schema class again which is missing the closing tag ")};" and there is no need to use Schema class again you can use the same object to add a foreign key to the table.
Try the below code :
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('category_id');
$table->string('title');
$table->text('content');
$table->boolean('is_show')->default(false);
$table->boolean('is_active')->default(false);
$table->integer('page_showing')->default(0);
$table->string('header_pic');
$table->softDeletes();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
OR
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('category_id');
$table->string('title');
$table->text('content');
$table->boolean('is_show')->default(false);
$table->boolean('is_active')->default(false);
$table->integer('page_showing')->default(0);
$table->string('header_pic');
$table->softDeletes();
$table->timestamps();
Schema::table('articles', function($table){
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
}); //closing Schema class tag
}); //closing Schema class tag
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
You dont need to alter table when you are creating the table to put foreigns.
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned(); // Or $table->unsignedInteger('user_id');
$table->integer('category_id')->unsigned();
$table->string('title');
$table->text('content');
$table->boolean('is_show')->default(0);
$table->boolean('is_active')->default(0);
$table->integer('page_showing')->default(0);
$table->string('header_pic');
$table->softDeletes();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});
However, you can do that correctly with this code
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('category_id')->unsigned();
$table->string('title');
$table->text('content');
$table->boolean('is_show')->default(0);
$table->boolean('is_active')->default(0);
$table->integer('page_showing')->default(0);
$table->string('header_pic');
$table->softDeletes();
$table->timestamps();
});
Schema::table('articles', function($table){
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});
You're missing the closure on Schema::create
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('category_id');
$table->string('title');
$table->text('content');
$table->boolean('is_show')->default(false);
$table->boolean('is_active')->default(false);
$table->integer('page_showing')->default(0);
$table->string('header_pic');
$table->softDeletes();
$table->timestamps();
});
Related
I am trying to make a laravel migration & establishing an foreign key constrain between two tables.
Here is some of my codes:
create_user_education_table
class CreateUserEducationTable extends Migration
{
public function up()
{
Schema::create('user_education', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')
->references('id')
->on('users')
->onUpdate('CASCADE')
->onDelete('CASCADE');
$table->unsignedBigInteger('institution_id');
$table->foreign('institution_id')
->references('id')
->on('education_institutions')
->onUpdate('CASCADE')
->onDelete('CASCADE');
$table->string('degree')->nullable();
$table->string('grade')->nullable();
$table->year('start_year')->nullable();
$table->year("end_year")->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::table('user_education', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropForeign(['institution_id']);
});
Schema::dropIfExists('user_education');
}
}
create_educational_institution_table
class CreateEducationInstitutionsTable extends Migration
{
public function up()
{
Schema::create('education_institutions', function (Blueprint $table) {
$table->id();
$table->string('institution_name');
$table->string('country');
$table->string('city');
$table->string('logo')->nullable();
$table->text('description')->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('education_institutions');
}
}
while migrating ```user_education_table`` it throwing following error.
What's the point here ?
you must create education_institutions table before of creating user_education table
**This is the Migration file which is created By Spatie and I want to use spatie for the admin panel and every tutorial having the usage for the User model and I have both user and admin model. I did not understand how to connect this with my admin model or admin db table because by default it is taking the user model and user table **
class CreatePermissionTables extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
$tableNames = config('permission.table_names');
$columnNames = config('permission.column_names');
if (empty($tableNames)) {
throw new \Exception('Error: config/permission.php not loaded. Run [php artisan config:clear] and try again.');
}
Schema::create($tableNames['permissions'], function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('guard_name');
$table->timestamps();
});
Schema::create($tableNames['roles'], function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('guard_name');
$table->timestamps();
});
Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) {
$table->unsignedBigInteger('permission_id');
$table->string('model_type');
$table->unsignedBigInteger($columnNames['model_morph_key']);
$table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index');
$table->foreign('permission_id')
->references('id')
->on($tableNames['permissions'])
->onDelete('cascade');
$table->primary(['permission_id', $columnNames['model_morph_key'], 'model_type'],
'model_has_permissions_permission_model_type_primary');
});
Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) {
$table->unsignedBigInteger('role_id');
$table->string('model_type');
$table->unsignedBigInteger($columnNames['model_morph_key']);
$table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index');
$table->foreign('role_id')
->references('id')
->on($tableNames['roles'])
->onDelete('cascade');
$table->primary(['role_id', $columnNames['model_morph_key'], 'model_type'],
'model_has_roles_role_model_type_primary');
});
Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
$table->unsignedBigInteger('permission_id');
$table->unsignedBigInteger('role_id');
$table->foreign('permission_id')
->references('id')
->on($tableNames['permissions'])
->onDelete('cascade');
$table->foreign('role_id')
->references('id')
->on($tableNames['roles'])
->onDelete('cascade');
$table->primary(['permission_id', 'role_id'], 'role_has_permissions_permission_id_role_id_primary');
});
app('cache')
->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null)
->forget(config('permission.cache.key'));
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
$tableNames = config('permission.table_names');
if (empty($tableNames)) {
throw new \Exception('Error: config/permission.php not found and defaults could not be merged. Please publish the package configuration before proceeding, or drop the tables manually.');
}
Schema::drop($tableNames['role_has_permissions']);
Schema::drop($tableNames['model_has_roles']);
Schema::drop($tableNames['model_has_permissions']);
Schema::drop($tableNames['roles']);
Schema::drop($tableNames['permissions']);
}
}
I have migrations for Users and Projects like this:
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->bigInteger('last_used_society_id');
$table->bigInteger('last_used_project_id');
$table->rememberToken();
$table->timestamps();
});
and
Schema::create('projects', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('eplan_name')->nullable();
$table->bigInteger('society_id');
$table->timestamps();
//$table->foreign('user_id')->references('id')->on('users');
});
Schema::table('projects', function (Blueprint $table) {
$table->foreign('society_id')->references('id')->on('societies');
});
table Users is altered after to add foreigns:
Schema::table('users', function (Blueprint $table) {
$table->foreign('last_used_society_id')->references('id')->on('societies');
$table->foreign('last_used_project_id')->references('id')->on('projects');
});
and in model of User i have this:
public function ActualProject(){
return $this->belongsTo('App\Models\Project', 'users_last_used_project_id_foreign');
}
public function ActualSociety(){
return $this->belongsTo('App\Models\Society', 'users_last_used_society_id_foreign');
}
but when i try to call $user->ActualProject it return null
why using 'users_last_used_project_id_foreign'?
just use the column's name as it is ...
public function ActualProject(){
return $this->belongsTo('App\Models\Project', 'last_used_project_id');
}
Im using the package https://github.com/musonza/groups to added group system on my laravel app, but when I want to migrate I get the error:
Migration table created successfully.
Migrating: 2019_05_26_125854_create_groups_tables
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table `groupsystem`.`#sql-2678_26` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `groups` add constraint `groups_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade)
This is my 2 migrations table code:
<?php
use Illuminate\Support\Facades\Schema;
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->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
And also this is the migration table I get from installing the package:
That I mentioned in the link in the top of this post
This is the first time I try to use this package
And im trying tocreate a group system on my laravel application
Any other suggetions will be welcomed
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateGroupsTables extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('groups', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description')->nullable();
$table->string('short_description')->nullable();
$table->string('image')->nullable();
$table->string('url')->nullable();
$table->integer('user_id')->unsigned();
$table->boolean('private')->unsigned()->default(false);
$table->integer('conversation_id')->unsigned()->nullable();
$table->text('extra_info')->nullable();
$table->text('settings')->nullable();
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
Schema::create('group_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('group_id')->unsigned();
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->foreign('group_id')
->references('id')
->on('groups')
->onDelete('cascade');
});
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('body');
$table->string('type');
$table->integer('user_id')->unsigned();
$table->text('extra_info')->nullable();
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string('body');
$table->integer('user_id')->unsigned();
$table->integer('post_id')->unsigned();
$table->string('type')->nullable();
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users');
$table->foreign('post_id')
->references('id')
->on('posts');
});
Schema::create('group_post', function (Blueprint $table) {
$table->integer('group_id')->unsigned();
$table->integer('post_id')->unsigned();
$table->timestamps();
$table->foreign('group_id')
->references('id')
->on('groups')
->onDelete('cascade');
$table->foreign('post_id')
->references('id')
->on('posts')
->onDelete('cascade');
});
Schema::create('likes', function (Blueprint $table) {
$table->integer('user_id')->index();
$table->integer('likeable_id')->unsigned();
$table->string('likeable_type');
$table->primary(['user_id', 'likeable_id', 'likeable_type']);
$table->timestamps();
});
Schema::create('reports', function (Blueprint $table) {
$table->integer('user_id')->index();
$table->integer('reportable_id')->unsigned();
$table->string('reportable_type');
$table->primary(['user_id', 'reportable_id', 'reportable_type']);
$table->timestamps();
});
Schema::create('group_request', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned()->index();
$table->integer('group_id')->unsigned()->index();
$table->timestamps();
$table->foreign('group_id')
->references('id')
->on('groups')
->onDelete('cascade');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('groups');
Schema::drop('group_user');
Schema::drop('posts');
Schema::drop('comments');
Schema::drop('group_post');
Schema::drop('likes');
Schema::drop('reports');
Schema::drop('group_request');
}
}
If you have used laravel 5.8 then
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
If you have used laravel < 5.8 then
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
I did found the solution it's because of the new Laravel 5.8.3 it comes with unsignedBigInteger(id) instead of increments(id)
I have multi level relation in my app
Category
|products
|product_items
|product_group
|product_attributes
Category Model
public function products(){
return $this->belongsToMany(Product::class);
}
Product Model
public function items()
{
return $this->hasMany(ProductItem2::class);
}
Item Model
public function attribute()
{
return $this->belongsToMany(AttributeValue::class,'product_groups','item_id','attribute_id')->withTimestamps();
}
Attribute value
public function attributeName(){
return $this->belongsTo(Attribute::class,'attribute_id');
}
all relations works fine but now I need filter my product by example 'colors'
so I tried
return $this->where('parent_id',0)->whereHas('childrenRecursive')->with('products.items.attribute.attributeName')->get();
put of course it return all categories with all product and product items etc
here is the migration files
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('image');
$table->string('cover')->nullable();
$table->string('url')->nullable();
$table->string('cover');
$table->text('url_cover');
$table->text('description')->nullabel();
$table->unsignedInteger('parent_id');
$table->boolean('elite')->default(0);
$table->timestamps();
});
Schema::create('category_product',function (Blueprint $table){
$table->integer('category_id');
$table->integer('product_id');
$table->primary(['product_id','category_id']);
});
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('description');
$table->string('serial_number');
$table->float('price');
$table->integer('discount');
$table->enum('type',['pound','percentage']);
$table->timestamp('start')->nullable();
$table->timestamp('end')->nullable();
$table->string('image');
$table->unsignedInteger('shop_id');
$table->boolean('recommendation');
$table->boolean('published');
$table->integer('preparing_days')->unsigned();
$table->integer('visits')->defualt(0);
$table->unsignedInteger('priorty')->defualt(0);
$table->timestamps();
});
Schema::create('product_items', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('product_id');
$table->double('price');
$table->string('image');
$table->unsignedInteger('amount');
$table->timestamps();
});
Schema::create('product_groups', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('attribute_id');
$table->unsignedInteger('item_id');
$table->timestamps();
});
Schema::create('attribute_values', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('attribute_id');
$table->string('value');
$table->timestamps();
});
Schema::create('attributes', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
all i need to return the attribute 'color' values like ['red,blue,black'] with each category
seems complicated and need help.