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

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

Related

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.

Php artisan not working because i deleted the table in phpMyadmin , how to undo this?

I deleted a table directly in my phpMyAdmin and how when i run the command php artsan migrate:rollback , i get the following error:
Failed to open:stream: No such file or directory.
I already deleted the migration file from the migrations folder , so why am i getting this error ??
I was just going through the laravel migrations documentation HERE, and i saw the below command:
php artisan migrate:rollback --step=5
So i tried the below in my command line:
php artisan migrate:rollback --step=1
To rollback my last migration , but it does't seem to work. Thats the reason i deleted the table directly in the phpMyAdmin.
Below is the error i get now , when i run the php artsan migrate:rollback:
Please try the following commands, it will be fine.
To clear cache
php artisan cache:clear
To composer autoload
composer dump-autoload
Now restart your Laravel server and it will be fine.
It will be working by now. However, it is never recommended to delete a table and a migration file directly. If that's the case, Laravel wouldn't have used migration at all. Please take care next time.
Since you removed the file before rolling back, I would suggest to delete the entry manually from your migrations table.
Search for the full name and delete just the 1 entry.
DELETE FROM `migrations` where `name` = '[2017_01_09_xxx]';
However, if your migration was already committed and pushed to remote and other people are working on the same codebase, I would suggest restoring the table on your local env and also restore the migration file with that name and remove the table by adding a new migration.
This flow will make sure that when other people work with your changes or that when you deploy and run migrations, you are able to do so without issues.

Laravel Artisan - reload .env variables or restart framework

I'm trying to write a command as a quickstart to build a project. The command asks for input for database details and then changes the .env file. The problem is that the command needs to do some Database queries afterwards, but the .env variables are not reloaded.
My question would be, is there a way to reload or override .env variables runtime. And if not, is there a way to call another Artisan command freshly, so framework bootstraps again?
In my command I tried doing $this->call('build:project') in my actual command, but even in the second call the variables are not reloaded.
Is there a way to achieve this without making the user manually call multiple commands?
Thanks!
i had the same problem with reloaded .env variables, and i fixed it with this command line which allow you to clear the configuration :
php artisan config:clear
Hope that helped. Regards.
Laravel uses config files which take data from .env file. So, what you can do is to override configuration values at runtime:
config(['database.default' => 'mysql']);
Try to clear cache it helped me (couldn't ssh into the server)
Is there a {app route}/bootstrap/cache/config.php file on the production server? Delete it.
This helped me
As OP I'm trying to bootstrap a Laravel project build by running console command and asking for the database credentials in the middle of the process.
This is a tricky problem and nothing I read was able to fix it : reset config, cache, Dotenv reload, etc... It seems that once the console command / operation is initialized the initial database connection is kept all over until the end.
The working solution I found is to bypass, after database modification are done, this cached state by using native shell exec command and passing the php artisan command as parameter :
passthru('php artisan migrate');
So, the order will be :
php artisan project:build (or whatever is your console command)
prompt the user for database credentials
replace values in .env file (your search & replace algorythm)
run php artisan config:cache
passthru('php artisan migrate'); ro run your migrations
shell_exec would do the same but in silent mode while passthru will return the output generated by console.
https://www.php.net/manual/en/function.passthru.php
Done successfully on Laravel 8.52

Select from table using command prompt (not browser) in Laravel framewok

I want to select all data from 1 table in command prompt (not from browser) using Laravel framework. Is there anyone know what the good references is? please give me link, thankyou
You can create an Artisan Command.
Check the Laravel 5.0 docs:
http://laravel.com/docs/5.0/commands
Then, you can run you command. Example:
php artisan mycommand:list
Laravel 4.2:
http://laravel.com/docs/4.2/commands
You can run artisan commands from "cmd" (Windows). (Edit your environment variables first! Add you PHP path).
Try:
php artisan tinker
Then:
DB::table('users')->get()

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