**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']);
}
}
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
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 am trying to migrate to a new DB in a different host. However, when I run php artisan migrate, it's giving me an error. Laravel is looking for a specific table(permissions table in this case, I have eloquent relationships set up) in my app. Obviously, a new DB is completely empty. How can I run the migration to a new DB in a new host? What am I missing here?
In Connection.php line 664:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'cf.permissions' doesn't exist (SQL: select * from `permissions`)
In PDOConnection.php line 63:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'cf.permissions' doesn't exist
In PDOConnection.php line 61:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'cf.permissions' doesn't exist
Permission Migration File;
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePermissionRoles extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('label');
$table->timestamps();
});
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('label');
$table->timestamps();
});
Schema::create('permission_role', function (Blueprint $table) {
$table->integer('permission_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->foreign('permission_id')
->references('id')
->on('permissions')
/* ->onDelete('cascade') */
;
$table->foreign('role_id')
->references('id')
->on('roles')
/* ->onDelete('cascade') */
;
$table->primary(['permission_id', 'role_id']);
});
Schema::create('role_user', function (Blueprint $table) {
$table->integer('role_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->foreign('role_id')
->references('id')
->on('roles')
/* ->onDelete('cascade') */
;
$table->foreign('user_id')
->references('id')
->on('users')
/* ->onDelete('cascade') */
;
$table->primary(['role_id', 'user_id']);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('permissions');
Schema::dropIfExists('role_user');
Schema::dropIfExists('permission_role');
Schema::dropIfExists('roles');
}
}
AppServiceProvider File;
public function boot()
{
$this->registerPolicies();
foreach ($this->getPermissions() as $permission) {
Gate::define($permission->name, function ($user) use ($permission) {
return $user->hasRole($permission->roles);
});
}
}
protected function getPermissions()
{
return Permission::with('roles')->get();
}
In your getPermissions() method, check if your schema permissions and roles exists and return values accordingly using Schema's hasTable() method. This way migrate commands would not hinder with service provider's methods.
Note that once the 2 tables are set(after a successful migration), this action from your service provider will always be executed.
<?php
protected function getPermissions(){
if(Schema::hasTable('permissions') && Schema::hasTable('roles')){
return Permission::with('roles')->get();
}
return collect([]);
}
I get the error:
'date_subscribed' doesn't have a default value
When running this SQL:
insert into `companys` (`title`, `idNo`, `expiration_date`, `slug`, `updated_at`, `created_at`)
This is my migration file:
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('companys', function (Blueprint $table) {
$table->increments('id');
$table->string('title',150)->unique();
$table->string('slug',150);
$table->varchar('idNo');
$table->date('date_subscribed');
$table->date('expiration_date');
$table->timestamps();
});
}
/**
* Reverse the migrations.
Please try this may help you:
Use Carbon class on the top of file Like this use Carbon\Carbon;
public function up()
{
Schema::create('companys', function (Blueprint $table) {
$table->increments('id');
$table->string('title',150)->unique();
$table->string('slug',150);
$table->varchar('idNo');
$table->date('date_subscribed')->default(Carbon::now()->toDateString());
$table->date('expiration_date')->default(Carbon::now()->toDateString());
$table->timestamps();
});
}
use nullable() or default(\Carbon\Carbon::now()->toDateString()) for this
public function up()
{
Schema::create('companys', function (Blueprint $table) {
$table->increments('id');
$table->string('title',150)->unique();
$table->string('slug',150);
$table->varchar('idNo');
$table->date('date_subscribed')->nullable();
$table->date('expiration_date')->nullable();
$table->timestamps();
});
}
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();
});