tread
tread

Reputation: 11098

How to run migrate on homestead when running migrate locally with Laravel?

I have to run migrations on the homestead box by SShing in:

homestead ssh
cd ~/Code/my-project
php artisan migrate

I would prefer to just run migrate on the local folder and automatically have the migrations run on the guest (virtual) machine.

Upvotes: 7

Views: 5227

Answers (5)

sardar1592
sardar1592

Reputation: 41

Actually you also need to pay attention to this part in the documentation:

You should only use these non-standard ports when connecting to the databases from your host machine. You will use the default 3306 and 5432 ports in your Laravel database configuration file since Laravel is running within the virtual machine.

This worked for me with Laravel 7.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=sardar
DB_USERNAME=homestead
DB_PASSWORD=secret

Upvotes: 1

Stefan Avramovic
Stefan Avramovic

Reputation: 1353

This worked for me, i created a database:laravel and changed DB_PORT=3306 to DB_PORT=33060 And it worked.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=33060
DB_DATABASE=laravel
DB_USERNAME=homestead
DB_PASSWORD=secret

also visit: https://laravel.com/docs/5.8/homestead#connecting-to-databases for documentation about

Upvotes: 1

Ioannis Chrysochos
Ioannis Chrysochos

Reputation: 448

When you properly install Homestead you must have a local directory with the Laravel application. So, there is no need to enter ssh in order to change files or run artisan commands.

You can use a terminal and go to the local directory of Laravel application. From there you can use artisan commands. Use the simple form "php artisan" to see that it is working.

The trick to have this local application directory (same as the one in Homestead VM) is to create it in advance and the installation of Laravel Homestead will fill that local directory too. The name of the local directory must be in Homestead.yaml in folders section as shown below.

folders: - map: d:/Homestead_Projects to: /home/vagrant/code

Since you run php scripts locally you must have an updated version of php installed in your computer. You can have local mysql server in your local computer too. When you do local database migrations you update the local database. If you want to update the database inside Homestead you must use ssh. So, you have two different set of data. If you use Laravel application code with a local web server you can use the data of the local database. When you use the web server of the Homestead (nginx) you use the Homestead database data.

In order to have a local web server (localhost:8000) you can run "php artisan serve". Remember to have your local database active. So, you have the same code, same schema, but different data in databases.

Upvotes: 1

Meir Cohen
Meir Cohen

Reputation: 406

The command you're looking for is: homestead ssh -c "cd ~/Code/my-project; php artisan migrate; exit".

Try to run it from your local console for testing it.

If it works, all you need is to create an alias for "migrate" that runs the above, and that's it.

Upvotes: 3

JuanDMeGon
JuanDMeGon

Reputation: 1211

By default, Laravel uses the localhost as the database host, just go to your homestead.yaml file and check the first line for the IP address (possibly is => ip: "192.168.10.10"). Use this IP as the host in your .env file:

In the .env file use: DB_HOST=192.168.10.10

Instead of DB_HOST=localhost

It must works.

PS: Homestead redirect the ports also, so possibly you need to change the port of the database to: 33060

In the .env file, use: DB_PORT=33060, instead of DB_PORT=3306

But, check first with the firt configuration.

Best wishes.

Upvotes: 10

Related Questions