Laravel abruptly truncating my database table - php

I am using version control for my project. After some updates and commit when I run my project again, I have observed that laravel is truncating my Category table.
There are two ways to seed table.
DatabaseSeeder and with php artisan db:seed and
Specific table seeder class by using
php artisan db:seed --class="CategoriesTableSeeder"
I am not able to catch behavior when its truncating my table.
I have tested it by commenting all line in the public function run(){} inside DatabaseSeeder class and also by commenting DB::table('categories')->insert($categories); and DB::table('categories')->truncate(); in CategoriesTableSeeder class.
Because table is getting truncated automatically, I have to seed it again and again.
Any idea???

To solve this problem have gone through all the possibilities. Searched through out the migration files and seed files everything was file. Its already more than 10 days, I was unable to resolve. Gone through the users comments also. But all that did not worked.
One day, I was checking database architecture and I found that engine for categories table was "Memory".
By default I had my database engine set to "InnoDB".
Changed it to "InnoDB" and Problem gone.

Related

Problem with Laravel spatie/laravel-permissions ( HasRole in Model User )

I have a problem with the spatie/laravel-permissions library.
Previously I had it implemented in my system but after doing composer update it stopped working...
The problem is when I add the HasRole in my User model. Everything crashes and I get the error:
"Call to a member function first() on array "
Making mention of the PermissionRegistrar package file.
Likewise, if I try to enter another route in my system, the error that appears is "
Undefined index: name"
It should be noted that I have my model created, my tables in the database and I carry out the package installation process following the documentation, and as I said before, the roles and permissions system worked for me before.
Something I should mention is that I had previously replaced the "name" field with "description" but I had some configuration problems with the library so I ran another migration adding the Name field, which the library requires. After running the migration, everything seemed to work correctly
You probably have a cache issue specifically related to Spatie. If you face any kind of issues when you are seeding your DB, you can add this line at the top of your seed within the run() method
app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();
On the other hand, if you changed something manually, let's say from DB, you can try with artisan command
php artisan permission:cache-reset
https://spatie.be/docs/laravel-permission/v5/basic-usage/artisan#content-resetting-the-cache
Note that you need to clear cache even if you are running a fresh install by using
php artisan migrate:fresh --seed
So you just ran a full update of all packages?
That is kind of a scary situation, I don't think I've ever done a full update to all packages.
I'm not sure what version of spatie you are coming or going with, that might help.
When I did a full update from Laravel 5.7 all the way to Laravel 8, spatie was a pain. Not just a little pain but a full on work for 4 days updating pain. All the database tables were renamed, more added. Then I had to write code to transfer 20k users and permissions over from the old tables to the new tables with correct relationships. Then I had to go though and use the newer functions...etc. Nightmare.
So as you can see you are a little vague in your question for a proper answer.

Is there a way to clear model tables using Laravel artisan?

During development, for testing purpose, I sometimes need the tables related to one of the model clear. For example, I may have a team model with testers, coders, managers, projects and offices models linked with foreign key. I would like to clear all of these tables to test my create team function.
Currently I do that by,
php artisan migrate:refresh --seed
It works, seeder adds back data I needed for the testing environment, but it also clears all other unrelated tables. Also, it drops and recreate table and therefore it is slow.
I know I can write a function for that, but it seems a bit overkill. Is there other artisan command for me to do this action?
All you need is this artisan command:
php artisan db:seed --class=TeamSeeder
where TeamSeeder is the name of the seeder class which you have created to seed a specific table.
Make sure you truncate the table in that seeder class, before seeding.
This command will not recreate the table and which is not even required in your case.
Hope this will help.

Laravel 4 migrate:make not working; just stops after "Compiling views" step

I'm a total newbie following the guestbook tutorial at laravelbook.com, via Koding.com.
In my project directory, running php artisan migrate:make create_entries_table (also tried with --create=entries appended) produces the following response:
Generated migration: blah_blah_blah_create_entries_table
Compiling common classes
Compiling views
That's it. No migration table or file is created. Any idea as to why this is happening? I already asked on the L forum yesterday, nobody's responded :( Would really appreciate some insight on this...
UPDATE:
Very odd. I ran migrate:reset, and it said Nothing to rollback. But now two migration files are there, from yesterday (one with schema blueprint, one just with blank up & down functions)! No rows in migrations table though.
Other than the migration files not appearing, this is expected behaviour. The migrate:make command only creates the migration file in which you will specify what database actions Laravel is supposed to trigger when migrating (up) or rolling back (down). By default that file will contain a class (which is named by studly-casing the migration name) which up and down methods, nothing more. You might want to use Laravel's Schema class, as per usual, or you might want to do raw queries using the DB class, but you're actually free to put whatever code you want to automate in there (I sometimes run artisan commands in my migrations). Since no error is triggered, I'm assuming Laravel has permission to create files in your database/migrations folder, so it might just be that your app is not refreshing the files view afterwards?
Anyway, in order to actually run the migrations, which will create the migrations table in your database if there isn't one already, is just php artisan migrate. To rollback the last batch of migrations you can use php artisan migrate:rollback and to rollback all batches you use php artisan migrate:reset, but either command is useless if no migration table yet exists, which appeared to be your case.

Laravel - Migration scripts used multple times across platforms

I just had a thought of serving fresh data for a demo app.
What I want to achieve is to have the schema and a set of data prepared beforehand and got ready to be migrated as the demo app is run. Simply put, every time a user runs the app, the database will be dumped with fresh data.
Is that possible? In Laravel 4, I know when I generate a migration script through the CLI, it seems like it has a timestamps attached to the file name, I was wondering if that would prevent the migration from being executed in later days in the future?
Thanks everyone.
What I want to achieve is to have the schema and a set of data prepared beforehand and got ready to be migrated as the demo app is run. Simply put, every time a user runs the app, the database will be dumped with fresh data.
Speaking of having a set of prepared data, I think Database Seeding is close to what you want.
The docs also mention how you could use the migration and seeding together. Although you'll need to decide how to do the migration + seed yourself. E.g. trigger by artisan command, trigger every page load, etc.
Rollback all migrations and run them all again (reference):
php artisan migrate:refresh
php artisan migrate:refresh --seed
You could also use packages like fzaninotto/Faker to help with generating a closer-to-real-life seed data for you.
In Laravel 4, I know when I generate a migration script through the CLI, it seems like it has a timestamps attached to the file name, I was wondering if that would prevent the migration from being executed in later days in the future?
The timestamp in the migration file name is only used for indicating the order in which the migration should be run. So you don't end up running migrations where you try to add a column before adding its table. As long as you do php artisan migrate:refresh and you have set up the up() and down() in your migration files correctly, it should not be a problem.
Although note that if you are in a team, be sure to check the migration order since two or more members can create migration files in parallel to each other.

laravel 4 - change column type with migration generator?

I am trying to create all files related to the table/db migration throughout the CMD with laravel generators and other tools.
And i am looking for to change column type with cmd line similar to this one:
$ php artisan generate:migration add_username_to_users_table
--fields="username:string"
i use this one to add username field - i believe its from the generator build for laravel 4. so i wonder if there is similar command in order to change column type to integer.
if someone have spread cheat with all the possible commends that would be great - thanks!
As you know, the Artisan CLI will create a migration file and if you notice, the file's name will be something like 2014_05_12_200322_create_users_table.php, which is the date and time of creation. If you run the command again with a changed column type, it will just create a new migration file for you.
This is a good thing and is something desired because otherwise it could get quite confusing keeping track of something that has been run or changed, especially when working in working in a team. So, use the rollback command and change your migration file as per your requirements and run the migration again.
migrate:rollback
Edit: Here's an article you should check out.
Have a look at Jeffrey Way's Migrations Generator
https://github.com/JeffreyWay/Laravel-4-Generators#migrations
extends migrations with many handy tools, including what you are looking for

Categories