Laravel spatie table column extend - php

In role_has_permissions table have 3 column, I want to add one more column. The column name is company id
$table->unsignedBigInteger('permission_id');
$table->string('model_type');
$table->unsignedBigInteger($columnNames['model_morph_key']);
Any way to add this

#MDMasum If I understand what you are asking right, then I believe you could add another column by generating another migration on the existing table. I am not familiar with Spatie, but I knowing Laravel, I don't see why this would not work.
You could do so with the following artisan command in CLI:
php artisan make:migration add_company_id_to_role_has_permissions_table --table=role_has_permissions
You will get a generated migration file that is named something similar to this:
2020_09_03_012345_add_company_id_to_role_has_permissions_table.php
Then modify the up function in that generated migration file accordingly.
Finally, run the following artisan command in CLI:
php artisan migrate

Related

Change Laravel Migration Status

I have mistakenly deleted the records of migration table in Laravel 5.5.
Now when i run php artisan migrate:status it shows the status of every migration as 'Not run'. How do the change the status of migration to 'Y'. I don't want run migrations that already ran before or run migrate:refresh or migrate:rollback.
Using backup of the database, i have also imported the records of migration table but still the status does not changes.
Migrations table has 3 columns: id, migration and batch. In my migrations table batch is always 1. Column migration contains the file names of the migration classes, so for example, file 2014_10_12_000000_create_users_table.php will be 2014_10_12_000000_create_users_table in the migration column.
So from this information you can generate your own sql query to populate the migrations table.
I suggest starting by cd into your migrations folder
cd project_name/database/migrations
and then running
ls > ./migrations.sql
From there you can edit the migrations.sql file and make it into an INSERT INTO statement. I haven't tested this so I can't claim this will work.

Run php artisan db:seed more than one and continue for duplicate keys insert

I create a seeder for insert default values in database.
If i run this seeder more than one time mysql return error for duplicate key,
So my question is that what is best approach to handle this error? And How can continue to run other seeds?
You shouldn't run db:seed command multiple times. A better way is to recreate all tables and seed the data with this command:
php artisan migrate:refresh --seed
Or just run db:seed once after running the php artisan migrate:refresh command.
https://laravel.com/docs/5.5/migrations#rolling-back-migrations
You can still use truncate method before seeding data, this will remove duplicate key errors because the table is already empty:
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class EntitiesTableSeeder extends Seeder {
public function run() {
DB::table('table')->truncate();
//OR
\App\Model::truncate();
// then insert your data here
}
Separate your seeds to more files and in database/DatabaseSeeder.php just call them like so:
$this->call(AuthorSeeder::class);
$this->call(ContentSeeder::class);
But sure, you cannot duplicate keys, thats the issue you have to solve.
eighty8/laravel-seeder
This package solves the problem, its version controlling the seeding like laravel does for migration, has some other benefits
Allows you to seed databases in different environments with different values.
Allows you to "version" seeds the same way that Laravel currently handles
migrations. Running php artisan seed will only run seeds that haven't already been run.
Allows you to run multiple seeds of the same model/table
Prompts you if your database is in production

laravel seed rollback after seeding database

I have seeded my DB using php artisan db::seed. Is there a way to rollback what I have seeded into my DB?
I cannot seem to find any command like php artisan db::seed rollback.
use Undo Seeder for Laravel.
When you install UndoSeeder, the following artisan commands are made available:
db:seed-undo Undo seeds in the seeds directory.
db:seed-refresh Undo seeds run seeds again.
more Undo-Seeder
You may also seed your database using the migrate:refresh command, which will also rollback and re-run all of your migrations. This command is useful for completely re-building your database:
php artisan migrate:refresh --seed
Running Seeders
If you want to wipe out certain table, then just TRUNCATE that table, and seed it again:
php artisan db:seed --class=UsersTableSeeder
No need for additional packages for such a simple task.
The easiest method is to go into your
database/seeders
folder and manually delete the files you want to remove then run php artisan migrate:fresh.
Open the database table with whichever platform you are using (phpMyAdmin / mySql-Workbench / a DB-editor plug-in etc.) and manually delete the seeded contents. Then you will be able to reseed the table using php artisan db:seed
I was looking for something else like i have ran php artisan db:seed and after that I wanted to change something in UserSeeder, like changing an email address.
So if you want to change something in Seeder class and you have already run db:seed command.
Then first of all you have to add truncate function before any other code like if you have UserSeeder class then add below code in run function before seeding all User model:
User::truncate();
Then all you have to do is re-run the command.
php artisan db:seed
It will seed again all the classes as per your change and delete already seeded Users in Database, You can use this method for any model you want to truncate the table and re insert the records.

How could I migrate my database with "php artisan migrate" in laravel

I clone my project from gitlab and I make it on my new computer.
But I get nothing ouput after I run php artisan migrate and the corresponding table is not created.
Output of php artisan migrate:status below.
What the meaning of "Ran?" and why status my migration file are all "N". php artisan migrate will create these tables successfully only when the "Ran?"s are "Y"?
An Y in the Ran column means the migration has been done.
Check your .ENV file , make sure your database settings are correct, also make sure you actually created the database beforehand.
clear the entire laravel cache to stay up to date with current .env settings, it might help.
try using php artisan migrate:refresh sometime it happens with me and i find out it's already migrated

Laravel 4 migration

i have created my database , then i have created a table with
php artisan migrate: make create_users_table
a file has created so i modified up and down function and i typed
php artisan migrate
until now everything is ok the problem started when i tried to add
another lignes to my database , i added another information about
users and when i type
php artisan migrate
the response was Nothing to migrate and there is no change into my databse ?
3 possibilities:
Create a new migration
Rollback the last migration operation and rerun it :
php artisan migrate:rollback
php artisan migrate
Rollback all migrations and run them all again
php artisan migrate:refresh
More info here: http://laravel.com/docs/migrations
You have to create another migration, or you can refresh your previous migrations
php artisan migrate:refresh
http://laravel.com/docs/migrations
If you just added more records in a table that already exists using e.g. phpmyadmin, there is no need to run again php artisan migrate, because no extra migration is created. That's why you get the "Nothing to migrate" message.
But, if you want for example to add a new column or delete an existing one, the ideal way to do it is by creating a new migration and name it something like add_columnname_on_users_table, edit properly the new migration file, then run php artisan migrate.

Categories